简体   繁体   English

表中每个具体类型配置的抽象类

[英]abstract classes in table per concrete type configuration

I have this simple situation: 我有一个简单的情况:

public abstract class Parent
{
    [Key]
    public Guid SomeId { get; set; }
}

public abstract class ParentArc
{
    public Guid ArcId { get; set; }

    public Guid StartNodeId { get; set; }
    public Guid EndNodeId { get; set; }

    public Parent StartNode { get; set; }
    public Parent EndNode { get; set; }
}

public class Node : Parent
{

}

public class Arc : ParentArc
{

}

And use these EntityTypeConfigurations: 并使用以下EntityTypeConfigurations:

public class ArcMapping : EntityTypeConfiguration<Arc>
{
    public ArcMapping()
    {
        HasKey(t => t.ArcId);
        HasRequired(p => p.StartNode).WithMany().HasForeignKey(p => p.StartNodeId).WillCascadeOnDelete(false);
        HasRequired(p => p.EndNode).WithMany().HasForeignKey(p => p.EndNodeId).WillCascadeOnDelete(false);
        Map(m =>
        {
        m.MapInheritedProperties();
        m.ToTable("Arc");
        });
    }
}

public class NodeMapping : EntityTypeConfiguration<Node>
{
    public NodeMapping()
    {
        HasKey(t => t.SomeId); 
        Map(m =>
        {
        m.MapInheritedProperties();
        m.ToTable("Node");
        });
    }
}

public class ParentArcMapping : EntityTypeConfiguration<ParentArc>
{
    public ParentArcMapping()
    {
        Map(m => m.ToTable("ParentArc"));
        HasRequired(p => p.StartNode).WithMany().HasForeignKey(p => p.StartNodeId).WillCascadeOnDelete(false);
        HasRequired(p => p.EndNode).WithMany().HasForeignKey(p => p.EndNodeId).WillCascadeOnDelete(false); 
    }
}

public class ParentMapping : EntityTypeConfiguration<Parent>
{
    public ParentMapping()
    {
        Map(m => m.ToTable("Parent"));
    }
}

In my DBContext I have: 在我的DBContext中,我有:

public DbSet<Node> Nodes { get; set; }
public DbSet<Arc> Arcs { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Add<ForeignKeyNamingConvention>();

    modelBuilder.Configurations.Add(new ArcMapping());
    modelBuilder.Configurations.Add(new NodeMapping());
}

I have switched from Table per Type (TPT) to Table per Concrete Type (TPC) as this makes life easier during bulk import. 我已从“每种类型的表(TPT)”切换为“每种混凝土类型的表(TPC)”,因为这使批量导入期间的工作变得更加轻松。 The major downside is that TPC has no physical table for abstract classes and thus no foreign keys in the physical table dbo.Arc: 主要缺点是TPC没有抽象类的物理表,因此物理表dbo.Arc中没有外键:

在此处输入图片说明

Is there anyway to change this? 反正有改变吗?

You still need to add the mapping configuration for ParentArc and define the primary key and the relationships within there. 您仍然需要为ParentArc添加映射配置,并在其中定义主键及其关系。 The mapping for Arc does not need to define any relationships as it will inherit the base class's with the call to MapInheritedProperties() Arc的映射不需要定义任何关系,因为它将通过调用MapInheritedProperties()来继承基类。

public class ArcMapping : EntityTypeConfiguration<Arc>
{
    public ArcMapping()
    {
        Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("Arc");
        });
    }
}

public class NodeMapping : EntityTypeConfiguration<Node>
{
    public NodeMapping()
    {
        HasKey(t => t.SomeId);
        Map(m =>
        {
            m.MapInheritedProperties();
            m.ToTable("Node");
        });
    }
}

public class ParentArcMapping : EntityTypeConfiguration<ParentArc>
{
    public ParentArcMapping()
    {
        HasKey(t => t.ArcId);
        HasRequired(p => p.StartNode).WithMany().HasForeignKey(p => p.StartNodeId).WillCascadeOnDelete(false);
        HasRequired(p => p.EndNode).WithMany().HasForeignKey(p => p.EndNodeId).WillCascadeOnDelete(false);
    }
}

and then in your OnModelCreating 然后在您的OnModelCreating

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new ParentArcMapping());

    modelBuilder.Configurations.Add(new ArcMapping());
    modelBuilder.Configurations.Add(new NodeMapping());

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF Core实现了Table-Per-Concrete-Type,具有抽象基类的流畅映射 - EF Core implementing Table-Per-Concrete-Type with fluent mapping of abstract base class 后续行动:每种混凝土类型的性能/比例表 - Followup: Table Per Concrete Type Performance/Scaling 不从每种具体类型的表中检索数据 - Not retrieving data from Table per concrete type 每个具体类型的表格,表格作为上下文属性 - Table per Concrete Type with tables as context properties Enttiy Framework中的每个层次结构表和每个具体类型的表? - Table per Hierarchy and Table per Concrete type in Enttiy Framework? 抽象类和具体类 - getter 和 setter - Abstract and concrete classes - getters and setters EF6中带有复合键的每个混凝土类型(TPC)映射表 - Table per Concrete Type(TPC) mapping with Composite key in the EF6 首先使用EF代码进行继承-每个具体类型(TPC)的表 - Inheritance with EF Code First - Table per Concrete Type (TPC) 实体框架6中的每个具体类型的表(TPC)继承(EF6) - Table Per Concrete Type (TPC) Inheritance in Entity Framework 6 (EF6) 果园模型继承与Table-Per-Concrete-Type? - Orchard model inheritance with Table-Per-Concrete-Type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM