EF: select all entities of subclass (inheritance)

梦想与她 提交于 2019-12-11 07:16:36

问题


I am using the Database first approach and DbContext. I have several inheritance structures (TPH) in my data model. But DbContext only creates one DbSet for the base class, and none for the subclasses. How should I retrieve all Entities of a specified subclass?

I cannot write queries based on the mapping criteria, as i had to remove those fields from the data model.

Can I simply add a new DbSet to the Entities class (partial class) ?


回答1:


You can add new properties returning DbSets of your derived types into context's partial class or you can simply use OfType<DerivedType>() operator when querying sets created by the code generator.



来源:https://stackoverflow.com/questions/10697145/ef-select-all-entities-of-subclass-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!