public class Project { public string Id { get; set; } public string Name { get; set; } public virtual BaseInfo BaseInfo { get; set; } public virtual MoneyInfo MoneyInfo { get; set; } public Project() { this.Id = Guid.NewGuid().ToString(); this.BaseInfo = new BaseInfo(); this.BaseInfo.ProjectId = this.Id; this.MoneyInfo = new MoneyInfo(); this.MoneyInfo.ProjectId = this.Id; } } public class BaseInfo { public string ProjectId { get; set; } public string No { get; set; } public double Area { get; set; } public virtual Project Project { get; set; } } public class MoneyInfo { public string ProjectId { get; set; } public string Unit { get; set; } public double Total { get; set; } public virtual Project Project { get; set; } } public class ProjectEntityTypeConfiguration : EntityTypeConfiguration<Project> { public ProjectEntityTypeConfiguration() { ToTable("_Project"); HasKey(t => t.Id); // 这种配置必须手工先删除Info项,再删除Project HasOptional(p => p.BaseInfo).WithRequired(i => i.Project); HasOptional(p => p.MoneyInfo).WithRequired(i => i.Project); // 这种配置无法删除对象 //HasRequired(p => p.BaseInfo).WithRequiredDependent(i => i.Project); //HasRequired(p => p.MoneyInfo).WithRequiredDependent(i => i.Project); } } public class MoneyInfoEntityTypeConfiguration : EntityTypeConfiguration<MoneyInfo> { public MoneyInfoEntityTypeConfiguration() { ToTable("_MoneyInfo"); HasKey(t => t.ProjectId); } } public class BaseInfoEntityTypeConfiguration : EntityTypeConfiguration<BaseInfo> { public BaseInfoEntityTypeConfiguration() { ToTable("_BaseInfo"); HasKey(t => t.ProjectId); } }