最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - EF Core deserialize to derived class (perhaps .SelectAs<DerivedType>() ??) - Stack Overflow

programmeradmin2浏览0评论
public class Note {}
public class NoteViewModel : Note {}
public class NoteDTO : Note {}
dbContext.Set<Note>().Select(note => new NoteViewModel {
  prop1 = note.prop1,
  prop2 = note.prop2,
  ...
}).ToArray();

// or
dbContext.Set<Note>().Select(note => new NoteDTO {
  prop1 = note.prop1,
  prop2 = note.prop2,
  ...
}).ToArray();

I'm not sure EF Core will instantiate the Note object before instantiate and copy/deserialize the data into NoteViewModel object or not, but I believe it's not.

  1. Is there a way to "shortcut" the Select action above? Imagine if the Note has a lot of columns. Perhaps something like SelectAs<NoteViewModel>()
  2. I looking for a way to downcast the configured entity type to any derived type regardless configured with EF Core or not without instantiate the entity type.

Thanks

public class Note {}
public class NoteViewModel : Note {}
public class NoteDTO : Note {}
dbContext.Set<Note>().Select(note => new NoteViewModel {
  prop1 = note.prop1,
  prop2 = note.prop2,
  ...
}).ToArray();

// or
dbContext.Set<Note>().Select(note => new NoteDTO {
  prop1 = note.prop1,
  prop2 = note.prop2,
  ...
}).ToArray();

I'm not sure EF Core will instantiate the Note object before instantiate and copy/deserialize the data into NoteViewModel object or not, but I believe it's not.

  1. Is there a way to "shortcut" the Select action above? Imagine if the Note has a lot of columns. Perhaps something like SelectAs<NoteViewModel>()
  2. I looking for a way to downcast the configured entity type to any derived type regardless configured with EF Core or not without instantiate the entity type.

Thanks

Share Improve this question edited Mar 15 at 13:16 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 15 at 11:10 codetalecodetale 1017 bronze badges 3
  • 2 You might want to look into AutoMapper, specially the ProjectTo() method. – Progman Commented Mar 15 at 11:16
  • You can use a Raw SQL : learn.microsoft/en-us/ef/ef6/querying/raw-sql – jdweng Commented Mar 15 at 11:20
  • Are you trying to store the derived types in a "table per heirachy"? – Jeremy Lakeman Commented Mar 15 at 13:21
Add a comment  | 

1 Answer 1

Reset to default 3

Select is a projection so when you use:

dbContext.Set<Note>().Select(note => new NoteViewModel {
    Id = note.Id,
    prop1 = note.prop1,
    prop2 = note.prop2,
}).ToArray();

... as an example, even if Note has 30 columns, the resulting SQL would just involve SELECT Id, prop1, prop2 FROM Notes. It does not fetch all columns. This also applies to when you use a navigation property. If Note has a navigation property to a related table and you use Select() to access one column from that related entity, EF will join the tables, but only return that single specified column.

Mapping libraries like Automapper, Mapify, and Mapperly have IQueryable projection methods that work in the same way, giving you something like what you described with SelectTo<T>. With Automapper there is ProjectTo<NoteViewModel>(config) This takes a mapper configuration argument with any mappings that cannot be formed via convention.

发布评论

评论列表(0)

  1. 暂无评论