本文介绍了EF数据库首先与TPT继承只创建DbSet< T>用于基础物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EF6.1 EDMX(数据库优先)模型,我使用TPT继承几种类型(即Employee:Person),但是在生成的 Model.Context.cs class I仅对我的基本类型有$ code> DbSet< T> 类,而不是继承类。是否有任何需要(或可以)完成EDMX模型或T4模板以生成继承类的 DbSet< T>

解决方案

您可以通过

  context.People.OfType< Employee>()

或者你可以扩展上下文通过一个部分类来定义子类型DbSets的属性:

 部分类上下文
{
public DbSet<员工>员工{get;组; }
}

比修改t4模板容易得多,因为标准的t4模板可以改变在将来的版本中,您必须再次修改它们。


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?

解决方案

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.

这篇关于EF数据库首先与TPT继承只创建DbSet&lt; T&gt;用于基础物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 22:09