【问题标题】:Not able to perform the delete operation from one table to another table using multiple one-to-one relationship?无法使用多个一对一关系执行从一个表到另一个表的删除操作?
【发布时间】:2019-09-03 13:08:30
【问题描述】:
  1. 首先,我创建了一个包含表之间关系的 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);
  1. 我已经像这样创建了实体“项目” -
 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; }
    }
  1. 我已经像这样创建了实体“项目联系人”-
    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; }
    }
  1. 现在,我希望每次从“项目”类中删除对象时都删除“项目联系人”。
  2. 但是,每次我的项目被删除但项目联系人保持不变时。

【问题讨论】:

    标签: c# .net-core entity-framework-core


    【解决方案1】:

    你能添加你用来删除的代码吗?通常要删除实体以及与其他实体的关系,您必须在删除操作之前包含它们。例如:

    var project = context.Projects.Include(b => b.ProjectInvoiceContact).First();
    context.Remove(project);
    

    可能对您有所帮助的另一件事是,您有多个“ProjectContacts”,所以我认为这种关系应该是一对多的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2021-09-10
      • 2019-11-07
      相关资源
      最近更新 更多