EF Database First with TPT Inheritance only creates DbSet<T> for base clases

为君一笑 提交于 2019-12-11 12:24:25

问题


I have an EF6.1 EDMX (Database First) model and I am using TPT inheritance for several types (ie. Employee : Person), however, in the generated Model.Context.cs class I only have DbSet<T> classes for my base types and not the inheriting ones. Is there anything that needs to (or can) be done to either the EDMX model or the T4 templates to generate DbSet<T> for the inheriting classes as well?


回答1:


You can get the subtypes from the context by

context.People.OfType<Employee>()

or you can extend the context by a partial class in which you define properties for subtype DbSets:

partial class Context
{
    public DbSet<Employee> Employees { get; set; }
}

That's much easier than modifying the t4 template because the standard t4 templates can change in future releases, so you'll have to modify them again.



来源:https://stackoverflow.com/questions/22973186/ef-database-first-with-tpt-inheritance-only-creates-dbsett-for-base-clases

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