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:
反正有沒有改變呢?