每个层次结构的实体框架表未创建区分符 [英] Entity Framework Table per Hierarchy not creating Discriminator

查看:64
本文介绍了每个层次结构的实体框架表未创建区分符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几次迁移后,我创建了一个继承层次结构.现在,当我使用代码优先迁移来更新数据库时,代码优先不会自动创建鉴别字段.此后,我删除了表并重新创建了它(使用代码优先迁移),但没有任何运气.我唯一能想到的是,派生类中没有其他非虚拟"属性-创建继承结构是为了强制执行业务规则,该规则仅允许某些派生类型与另一个实体有关系./p>

基本类型:

 公共抽象类Process{私有ICollection< ProcessSpecification>_specifications {获取;放;}受保护的Process(){_specifications =新的List< ProcessSpecification>();}公共Int32 ID {放;}public String Description {get;放;}公共Int32 ToolId {放;}公共虚拟工具工具{放;}公共虚拟ICollection< ProcessSpecification>规格{得到{return _specifications;}设置{_specifications =值;}}} 

派生类(没有不同/唯一的标量属性):

 公共类AssemblyProcess:过程{私有ICollection< AssemblyProcessComponent>_组件;公共AssemblyProcess(){_components = new List< AssemblyProcessComponent>();}公共虚拟ICollection< AssemblyProcessComponent>组件{得到{return _components;}设置{_components =值;}}} 

另一种派生类型

 公共类MachiningProcess:过程{私有ICollection< MachiningProcessFeature>_特征;公共MachiningProcess(){_features = new List< MachiningProcessFeature>();}公共虚拟ICollection< MachiningProcessFeature>功能{取得{return _features;}设置{_features =值;}}} 

代码优先是否未在数据库中添加discriminator列,因为它看不到派生类之间的任何区别(因为没有任何唯一的非虚拟"属性)?如果是这样,我该如何解决?如果不是,为什么代码优先将不会在数据库中自动创建区分符列的原因有哪些?我还有另一个TPH结构,可以按照预期的方式工作.

DbContext:

  public LineProcessPlanningContext():base("LineProcessPlanning"){}公共DbSet< Component>组件{get;放;}公共DbSet< Customer>客户{放;}公共DbSet< Feature>功能{放;}公共DbSet< OperationDefinition>OperationDefinitions {放;}公共DbSet< PartDesign>零件设计{放;}公共DbSet< Process>流程{get;放;}公共DbSet< ProcessPlan>ProcessPlans {获取;放;}公共DbSet< ProcessPlanStep>ProcessPlanSteps {放;}公共DbSet< ProductionLine>生产线{get;放;}公共DbSet< StationCycleDefinition>StationCycleDefinitions {get;放;}公共DbSet< StationCycleStep>StationCycleSteps {放;}公共DbSet< StationDefinition>StationDefinitions {放;}公共DbSet< UnitOfMeasurement>UnitsOfMeasurement {get;放;}公共DbSet< Tool>工具{get;放;} 

我还尝试创建每种派生类型所独有的虚拟"属性.代码迁移将新属性作为列添加到表中,但是迁移未创建区分项列.

解决方案

在我的情况下,我发现了造成这种情况的原因,与您的情况相同.基类是 abstract ,因此EF不会为该类创建TPH表,因为它无法实例化.作为抽象基类的结果,EF将为每个派生类创建表,因此不需要区分列.

就我而言,从基类中删除抽象是可以接受的.一旦完成此操作,EF的TPH就会按预期工作.

I created an inheritance hierarchy after a few migrations. Now when I update the database using code first migrations, code-first is not automatically creating the discriminator field. I have since dropped the table and recreated it (using code-first migrations) without any luck. The only thing I can think of is that there are no additional "non-virtual" properties in the derived classes--the inheritance structure was created to enforce a business rule that only a certain derived type can have a relationship with another entity.

Base Type:

public abstract class Process
{

    private ICollection<ProcessSpecification> _specifications { get; set; }

    protected Process()
    {
        _specifications = new List<ProcessSpecification>();
    }        

    public Int32 Id { get; set; }
    public String Description { get; set; }
    public Int32 ToolId { get; set; }

    public virtual Tool Tool { get; set; }        
    public virtual ICollection<ProcessSpecification> Specifications
    {
        get { return _specifications; }
        set { _specifications = value; }
    }

}

Derived class (no different/unique scalar properties):

public class AssemblyProcess : Process
{
    private ICollection<AssemblyProcessComponent> _components;

    public AssemblyProcess()
    {
        _components = new List<AssemblyProcessComponent>();            
    }

    public virtual ICollection<AssemblyProcessComponent> Components
    {
        get { return _components; }
        set { _components = value; }
    }
}

Another derived type

public class MachiningProcess : Process
{
    private ICollection<MachiningProcessFeature> _features;

    public MachiningProcess()
    {
        _features = new List<MachiningProcessFeature>();
    }

    public virtual ICollection<MachiningProcessFeature> Features { get { return _features; } set { _features = value; } }
}

Is code-first not adding the discriminator column in the database because it doesn't see any differences between the derived classes (because of there not being any unique "non-virtual" properties)? If so, how do I get around this? If not, what are some reasons why code-first would not automatically create the discriminator column in the database? I have another TPH structure that works exactly the way it's supposed to.

DbContext:

public LineProcessPlanningContext()
        : base("LineProcessPlanning")
    {
    }   

public DbSet<Component> Components { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Feature> Features { get; set; }
public DbSet<OperationDefinition> OperationDefinitions { get; set; }
public DbSet<PartDesign> PartDesigns { get; set; }
public DbSet<Process> Processes { get; set; }
public DbSet<ProcessPlan> ProcessPlans { get; set; }
public DbSet<ProcessPlanStep> ProcessPlanSteps { get; set; }
public DbSet<ProductionLine> ProductionLines { get; set; }       
public DbSet<StationCycleDefinition> StationCycleDefinitions { get; set; }
public DbSet<StationCycleStep> StationCycleSteps { get; set; }
public DbSet<StationDefinition> StationDefinitions { get; set; }
public DbSet<UnitOfMeasurement> UnitsOfMeasurement { get; set; }        
public DbSet<Tool> Tools { get; set; }

I also tried creating "dummy" properties that are unique to each derived type. Code migrations added the new properties as columns to the table, but the migration did not create a discriminator column.

解决方案

I figured out the cause of this in my situation, same as yours. The base class is abstract, therefore EF won't create a TPH table for that class since it can't be instantiated. As a result of the abstract base class, EF will create tables for each of the derived classes, and therefore no need for a discriminator column.

In my case, it was acceptable to remove abstract from the base class. Once I did this, EF's TPH worked as expected.

这篇关于每个层次结构的实体框架表未创建区分符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆