[英]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.