【发布时间】:2019-09-03 13:08:30
【问题描述】:
- 首先,我创建了一个包含表之间关系的 DbContext 类
modelBuilder.Entity<Projects>()
.HasOne(p => p.ProjectDeliveryContact)
.WithOne(i => i.Delivery)
.HasForeignKey<ProjectContacts>(b => b.del)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Projects>()
.HasOne(p => p.ProjectInvoiceContact)
.WithOne(i => i.Invoice)
.HasForeignKey<ProjectContacts>(b => b.inv)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Projects>()
.HasOne(p => p.ProjectCommercialContact)
.WithOne(i => i.Commercial)
.HasForeignKey<ProjectContacts>(b => b.Com)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Projects>()
.HasOne(p => p.ProjectEscalationContact)
.WithOne(i => i.Escalation)
.HasForeignKey<ProjectContacts>(b => b.esc)
.OnDelete(DeleteBehavior.Cascade);
- 我已经像这样创建了实体“项目” -
public class Projects
{
[Key]
public int Id { get; set; }
public List<ProjectRisks> ProjectRisk { get; set; } //This object can be used to get/set
//project risk for a particular project
public List<StatementOfWork> statementOfWorks { get; set; } //This object can be used to get/set
//statement of work for a particular project
public string Name { get; set; }
public string ScopeSummary { get; set; }
public bool IsVerified { get; set; }
public bool Status { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public int? CustomerId { get; set; }
[ForeignKey("CustomerId")]
public virtual Customers Customer { get; set; } //This object can be used to get/set
//customer for a particular project
public int? OptimusDeliveryId { get; set; }
[ForeignKey("OptimusDeliveryId")]
public virtual OptimusContact OptimusDeliveryContact { get; set; } //This object can be used to get/set
//optimus contact of delivery type for a particular project
public int? OptimusSalesId { get; set; }
[ForeignKey("OptimusSalesId")]
public virtual OptimusContact OptimusSalesContact { get; set; } //This object can be used to get/set
//optimus contact of sales type for a particular project
public int? DeliveryId { get; set; }
[ForeignKey("DeliveryId")]
public virtual ProjectContacts ProjectDeliveryContact { get; set; } //This object can be used to get/set
//project contact of delivery type for a particular project
public int? CommercialId { get; set; }
[ForeignKey("CommercialId")]
public virtual ProjectContacts ProjectCommercialContact { get; set; } //This object can be used to get/set
//project contact of commercial type for a particular project
public int? InvoiceId { get; set; }
[ForeignKey("InvoiceId")]
public virtual ProjectContacts ProjectInvoiceContact { get; set; } //This object can be used to get/set
//project contact of invoice type for a particular project
public int? EscalationId { get; set; }
[ForeignKey("EscalationId")]
public virtual ProjectContacts ProjectEscalationContact { get; set; } //This object can be used to get/set
//project contact of escalation type for a particular project
public int? CreatedBy { get; set; }
[ForeignKey("CreatedBy")]
public virtual User CreatedByUser { get; set; }
public int? UpdatedBy { get; set; }
[ForeignKey("UpdatedBy")]
public virtual User UpdatedByUser { get; set; }
}
- 我已经像这样创建了实体“项目联系人”-
public class ProjectContacts
{
public int Id { get; set; }
public int? ContactTypeId { get; set; }
[ForeignKey("ContactTypeId")]
public virtual ContactType ContactType { get; set; } //This object can be used to get/set
//project contact for a particular contact type
public string Name { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
- 现在,我希望每次从“项目”类中删除对象时都删除“项目联系人”。
- 但是,每次我的项目被删除但项目联系人保持不变时。
【问题讨论】:
标签: c# .net-core entity-framework-core