Entity framework EF代码优先的方法:在EF外键约束中被流畅的语法所迷惑
我正在尝试使用EF代码优先的方法创建一个语法流畅的外键关系 我的实体如下:Entity framework EF代码优先的方法:在EF外键约束中被流畅的语法所迷惑,entity-framework,foreign-key-relationship,Entity Framework,Foreign Key Relationship,我正在尝试使用EF代码优先的方法创建一个语法流畅的外键关系 我的实体如下: public partial class Defect { public int DefectID { get; set; } public decimal ReleaseNo { get; set; } public int BuildNo { get; set; } public string Title { get; set; } public string Descript
public partial class Defect
{
public int DefectID { get; set; }
public decimal ReleaseNo { get; set; }
public int BuildNo { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string StepsToReproduce { get; set; }
public int ApplicationModuleID { get; set; }
public int SeverityLevel { get; set; }
public string LoggedBy { get; set; }
public Nullable<System.DateTime> LoggedOn { get; set; }
public string LastModifiedBy { get; set; }
public Nullable<System.DateTime> LastModifiedOn { get; set; }
public string AssignedTo { get; set; }
public string Status { get; set; }
public string ResolutionNote { get; set; }
public Nullable<System.DateTime> ResolvedOn { get; set; }
public int ProjectID { get; set; }
public virtual SeverityIndex SeverityIndex { get; set; }
public virtual User LoggedByUser { get; set; }
public virtual User LastModifiedUser { get; set; }
public virtual User AssignedToUser { get; set; }
public virtual Project Project { get; set; }
}
public class DefectMap:EntityTypeConfiguration<Defect>
{
public DefectMap()
{
this.HasKey(d => d.DefectID);
this.ToTable("Defect");
this.Property(d => d.DefectID)
.IsRequired()
.HasColumnName("DefectID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(d => d.Description)
.IsRequired()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000)
.HasColumnName("Description")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.StepsToReproduce)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(4000)
.HasColumnName("StepsToReproduce")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.LastModifiedBy)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(10)
.HasColumnName("LastModifiedBy")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.AssignedTo)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(10)
.HasColumnName("AssignedTo")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.Status)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(50)
.HasColumnName("Status")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.ResolutionNote)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(4000)
.HasColumnName("ResolutionNote")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.HasRequired(p => p.Project).WithMany(p => p.DefectList).HasForeignKey(p => p.ProjectID);
this.HasRequired(s => s.SeverityIndex).WithMany(s => s.DefectList).HasForeignKey(s => s.SeverityLevel).WillCascadeOnDelete();
this.HasOptional(u => u.AssignedToUser).WithMany(u => u.AssignedToUserList).HasForeignKey(u => u.AssignedTo).WillCascadeOnDelete();
this.HasOptional(u => u.LastModifiedUser).WithMany(u => u.ModifiedByUserList).HasForeignKey(u => u.LastModifiedBy);
this.HasRequired(u => u.LoggedByUser).WithMany(u => u.LoggedByUserList).HasForeignKey(u => u.LoggedBy);
}
public partial class Project
{
public Project()
{
ApplicationModuleList = new List<ApplicationModule>();
DefectList = new List<Defect>();
UserList = new List<User>();
}
public int ID { get; set; }
public string ProjectName { get; set; }
public string ProjectManager { get; set; }
public Nullable<System.DateTime> ProjectStartDate { get; set; }
public Nullable<System.DateTime> ProjectEstimatedEndDate { get; set; }
public Nullable<System.DateTime> ProjectActualEndDate { get; set; }
public Nullable<int> ProjectBillingModel { get; set; }
public Nullable<decimal> ProjectEstimatedBudget { get; set; }
public Nullable<decimal> ProjectActualBudget { get; set; }
public Nullable<int> ProjectPortfolio { get; set; }
public Nullable<decimal> ProjectBillingRate { get; set; }
public Nullable<int> ProjectEstimatedManHours { get; set; }
public Nullable<int> ProjectActualManHours { get; set; }
public Nullable<int> ProjectIsApproved { get; set; }
public virtual ICollection<ApplicationModule> ApplicationModuleList { get; set; }
public virtual ICollection<Defect> DefectList { get; set; }
public virtual ICollection<User> UserList { get; set; }
public virtual BillingModel BillingModel { get; set; }
public virtual Portfolio Portfolio { get; set; }
public virtual ApprovalStatus ApprovalStatus { get; set; }
}
public class ProjectMap:EntityTypeConfiguration<Project>
{
public ProjectMap()
{
this.HasKey(p => p.ID);
this.ToTable("Projects");
this.Property(p => p.ID)
.HasColumnName("ID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
this.Property(p => p.ProjectName)
.HasColumnName("ProjectName")
.HasMaxLength(200)
.IsRequired()
.IsVariableLength()
.IsUnicode()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.HasOptional(p => p.BillingModel).WithMany(p=>p.Projects).HasForeignKey(p => p.ProjectBillingModel).WillCascadeOnDelete();
this.HasOptional(p => p.Portfolio).WithMany(p=>p.Projects).HasForeignKey(p => p.ProjectPortfolio).WillCascadeOnDelete();
this.HasOptional(p => p.ApprovalStatus).WithMany(p=>p.Projects).HasForeignKey(p => p.ProjectIsApproved).WillCascadeOnDelete();
}
}
公共部分类缺陷
{
公共ID{get;set;}
公共十进制ReleaseNo{get;set;}
公共int BuildNo{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串StepStoreProduct{get;set;}
public int ApplicationModuleID{get;set;}
public int SeverityLevel{get;set;}
公共字符串LoggedBy{get;set;}
公共可为空的LoggedOn{get;set;}
公共字符串LastModifiedBy{get;set;}
公共可为空的LastModifiedOn{get;set;}
分配给{get;set;}的公共字符串
公共字符串状态{get;set;}
公共字符串解析说明{get;set;}
公共可为空的ResolvedOn{get;set;}
公共int ProjectID{get;set;}
公共虚拟服务器索引服务器索引{get;set;}
公共虚拟用户LoggedByUser{get;set;}
公共虚拟用户LastModifiedUser{get;set;}
公共虚拟用户分配的用户{get;set;}
公共虚拟项目{get;set;}
}
公共类缺陷映射:EntityTypeConfiguration
{
公共地图()
{
this.HasKey(d=>d.DefectID);
本条为不可更改(“缺陷”);
this.Property(d=>d.DefectID)
.IsRequired()
.HasColumnName(“缺陷ID”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(d=>d.Description)
.IsRequired()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000年)
.HasColumnName(“说明”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d=>d.StepStoreProduct)
.等民族
.IsUnicode()
.IsVariableLength()
.HasMaxLength(4000)
.HasColumnName(“StepStoreProduct”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d=>d.LastModifiedBy)
.等民族
.IsUnicode()
.IsVariableLength()
.HasMaxLength(10)
.HasColumnName(“LastModifiedBy”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d=>d.AssignedTo)
.等民族
.IsUnicode()
.IsVariableLength()
.HasMaxLength(10)
.HasColumnName(“转让给”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d=>d.Status)
.等民族
.IsUnicode()
.IsVariableLength()
.HasMaxLength(50)
.HasColumnName(“状态”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d=>d.ResolutionNote)
.等民族
.IsUnicode()
.IsVariableLength()
.HasMaxLength(4000)
.HasColumnName(“决议注释”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
HasRequired(p=>p.Project).WithMany(p=>p.DefectList).HasForeignKey(p=>p.ProjectID);
this.HasRequired(s=>s.SeverityIndex).WithMany(s=>s.DefectList).HasForeignKey(s=>s.SeverityLevel).WillCascadeOnDelete();
this.has可选(u=>u.AssignedToUser)。带有多个(u=>u.AssignedToUserList)。has foreignkey(u=>u.AssignedTo)。WillCascadeOnDelete();
this.HasOptional(u=>u.LastModifiedUser)。with many(u=>u.ModifiedByUserList)。HasForeignKey(u=>u.LastModifiedBy);
HasRequired(u=>u.LoggedByUser).WithMany(u=>u.LoggedByUserList).HasForeignKey(u=>u.LoggedBy);
}
公共部分类项目
{
公共工程()
{
ApplicationModuleList=新列表();
缺陷列表=新列表();
UserList=新列表();
}
公共int ID{get;set;}
公共字符串ProjectName{get;set;}
公共字符串项目管理器{get;set;}
公共可为空的ProjectStartDate{get;set;}
公共可为空的ProjectEstimatedEndDate{get;set;}
公共可为空的ProjectActualEndDate{get;set;}
公共可为空的ProjectBillingModel{get;set;}
公共可为空的ProjectEstimatedBudget{get;set;}
公共可为空的ProjectActualBudget{get;set;}
可为空的公共项目组合{get;set;}
公共可为空的项目Billingrate{get;set;}
公共可为空的ProjectEstimatedManHours{get;set;}
公共可空项目实际工时{get;set;}
公共可空项目已批准{get;set;}
公共虚拟ICollection应用程序模块列表{get;set;}
公共虚拟ICollection缺陷列表{get;set;}
公共虚拟ICollection用户列表{get;set;}
公共虚拟BillingModel BillingModel{get;set;}
公共虚拟投资组合{get;set;}
公共虚拟ApprovalStatus ApprovalStatus{get;set;}
}
公共类ProjectMap:EntityTypeConfiguration
{
公共项目地图()
{
this.HasKey(p=>p.ID);
本表为ToTable(“项目”);
this.Property(p=>p.ID)
.HasColumnName(“ID”)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
this.Property(p=>p.ProjectName)
.HasColumnName(“项目名称”)
.HasMaxLength(200)
.IsRequired()
.IsVariableLength()
.IsUnicode()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
HasOptional(p=>p.BillingModel).WithMany(p=>p.Projects).HasForeignKey(p=>p.ProjectBillingModel).WillCascadeOnDelete();
HasForeignKey(p=>p.ProjectPortfolio).WillCascadeOnDelete();
this.HasOptional(p=>p.ApprovalStatus).WithMany(p=>p.Projects).Has
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
WillCascadeOnDelete(false)