问题
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