簡體   English   中英

表中每個具體類型配置的抽象類

[英]abstract classes in table per concrete type configuration

我有一個簡單的情況:

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
{

}

並使用以下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"));
    }
}

在我的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());
}

我已從“每種類型的表(TPT)”切換為“每種混凝土類型的表(TPC)”,因為這使批量導入期間的工作變得更加輕松。 主要缺點是TPC沒有抽象類的物理表,因此物理表dbo.Arc中沒有外鍵:

在此處輸入圖片說明

反正有改變嗎?

您仍然需要為ParentArc添加映射配置,並在其中定義主鍵及其關系。 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);
    }
}

然后在您的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM