2015-07-10 76 views
1

我有這樣的簡單情況:抽象類表每個具體類型的配置

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:

enter image description here

反正有沒有改變呢?

回答

2

您仍然需要爲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()); 

} 
相關問題