我首先使用EF 6.1.1
代码.NET framework 4
,我有一个抽象基类BaseEntity
,其中所有其他实体都从中继承。{1}}我的模型中有以下课程:
public abstract class BaseEntity
{
public int Id { get; set; }
....
}
public class Document:BaseEntity
public class OrderHeader:Document
public class RequestHeader:Document
我想通过定义以下映射文件来更改EF默认约定映射:
public class RequestHeader_Mapping : EntityTypeConfiguration<RequestHeader>
{
public RequestHeader_Mapping()
{
this.ToTable("RequestHeader");
this.Property(t => t.Id).HasColumnName("DocumentId");
...
}
}
public class OrderHeader_Mapping : EntityTypeConfiguration<OrderHeader>
{
public OrderHeader_Mapping()
{
this.ToTable("OrderHeader");
this.Property(t => t.Id).HasColumnName("DocumentId");
...
}
}
[更新]
public partial class Document_Mapping : BaseEntity_Mapping<Document>
{
public Document_Mapping()
{
this.ToTable("Document");
this.Property(t => t.Id).HasColumnName("Id");
this.HasOptional(t => t.Creater).WithMany().HasForeignKey(d => d.CreatedById);
this.HasOptional(t => t.Modifier).WithMany().HasForeignKey(d => d.ModifiedById);
this.HasOptional(t => t.Owner).WithMany().HasForeignKey(d => d.OwnerId);
this.HasOptional(t => t.DocumentStatu).WithMany().HasForeignKey(d => d.DocumentStatusId);
this.HasOptional(t => t.DocumentStation).WithMany(t => t.Documents).HasForeignKey(d => d.DocumentStationId);
this.HasOptional(t => t.DocumentType).WithMany().HasForeignKey(d => d.DocumentTypeId);
this.HasOptional(t => t.DocumentFolder).WithMany().HasForeignKey(d => d.MainFolderId);
this.HasOptional(t => t.SecurityLevel).WithMany).HasForeignKey(d => d.SecurityLevelId);
this.Ignore(t => t.ToDocuments);
}
}
public partial class BaseEntity_Mapping<TEntity> : EntityTypeConfiguration<TEntity> where TEntity:BaseEntity
{
public BaseEntity_Mapping()
{
this.HasKey(e => e.Id);
this.Property(t=>t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Ignore(t => t.MustDelete);
this.Ignore(t=>t.PreviousState);
this.Ignore(t => t.State);
}
}
并在我的DbContext中以下列方式使用它们:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Configurations.Add(new RequestHeader_Mapping());
modelBuilder.Configurations.Add(new OrderHeader_Mapping());
modelBuilder.Configurations.Add(new Document_Mapping());
}
适用于RequestHeader
,但不适用于OrderHeader
,我的意思是RequestHeader
表的生成密钥为DocumentId
,但OrderHeader
为Id
{1}}但是,我真的很困惑,知道问题在哪里吗?
答案 0 :(得分:0)
我的OrderHeader
与另一个实体有一个零或一个关系,当我删除它时,问题就解决了!