简体   繁体   English

当Child具有WillCascadeOnDelete的外键时,实体框架无法删除父条目(false)

[英]Entity Framework Cannot Delete Parent Entry When Child Has Foreign Key With WillCascadeOnDelete(false)

Why I cannot delete the parent entry while the child of it is set with WillCascadeOnDelete(false) on the foreign key? 为什么我不能删除父项,而其子项是使用外键上的WillCascadeOnDelete(false)设置的?

This is the parent: 这是父母:

public class EA_Client
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Client Id")]
    public int ClientId { get; set; }

    [Display(Name = "User Id")]
    public int? UserId { get; set; }

    [Display(Name = "Client Name")]
    public string ClientName { get; set; }

    public virtual AlvinCMSExtension.Models.UserProfile User { get; set; }

    public EA_Client()
    {
        UserId = 0;
        ClientName = "";
    }
}

This is the child : 这是孩子:

public class EA_Order
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [Display(Name = "Order Id")]
    public int OrderId { get; set; }

    [Display(Name = "Client Id")]
    public int? ClientId { get; set; }

    [Display(Name = "Supplier Id")]
    public int? SupplierId { get; set; }

    [Display(Name = "Total Amount To Pay")]
    public decimal TotalAmountToPay { get; set; }

    [Display(Name = "Order Time")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime OrderTime { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    public virtual ICollection<EA_OrderStatus> OrderStatuses { get; set; }
    public virtual ICollection<EA_OrderDetail> OrderDetails { get; set; }
    public virtual EA_Client Client { get; set; }
    public virtual EA_Supplier Supplier { get; set; }

    public EA_Order()
    {
        ClientId = null;
        SupplierId = null;
        TotalAmountToPay = 0;
        OrderTime = DateTime.Now;
        Description = "";
    }
}

This is the model builder for the child : 这是孩子的模型构建器:

        mb.Entity<EA_Order>().HasOptional<EA_Client>(m => m.Client).WithMany().HasForeignKey(m => m.ClientId).WillCascadeOnDelete(false);
        mb.Entity<EA_Order>().HasOptional<EA_Supplier>(m => m.Supplier).WithMany().HasForeignKey(m => m.SupplierId).WillCascadeOnDelete(false);
        mb.Entity<EA_Order>().HasMany<EA_OrderStatus>(m => m.OrderStatuses);
        mb.Entity<EA_Order>().HasMany<EA_OrderDetail>(m => m.OrderDetails);

Everytime I tried to delete the parent (EA_Client), this error occurs : 每次我尝试删除父(EA_Client)时,都会发生以下错误:

{"The DELETE statement conflicted with the REFERENCE constraint \\"FK_EAccounting.EA_Order_EAccounting.EA_Client_ClientId\\". The conflict occurred in database \\"EAccountingDB\\", table \\"EAccounting.EA_Order\\", column 'ClientId'.\\r\\nThe statement has been terminated."} {“DELETE语句与REFERENCE约束冲突\\”FK_EAccounting.EA_Order_EAccounting.EA_Client_ClientId \\“。冲突发生在数据库\\”EAccountingDB \\“,表\\”EAccounting.EA_Order \\“,列'ClientId'。\\ r \\ n声明已被终止。“}

How to fix this? 如何解决这个问题? I want to be able to delete the parent (EA_Client) since the parent is optional, means it is ok to have the child (EA_Order) to be orphanage entry. 我希望能够删除父级(EA_Client),因为父级是可选的,这意味着可以将子级(EA_Order)作为孤儿院条目。

I believe that WillCascadeOnDelete should be specified for parent entity, not for child. 我认为应该为父实体指定WillCascadeOnDelete ,而不是为子实体。 Also if dependency is optional (as you have) default behavior should be keeping children on parent removing. 此外,如果依赖项是可选的(如您所有),则默认行为应该是将子项保留在父项删除上。 Did you try just remove your .WillCascadeOnDelete(false) and delete Client entity? 您是否尝试删除.WillCascadeOnDelete(false)并删除Client实体?

Update: 更新:

MSDN link to approve my statement. MSDN链接批准我的声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架父-&gt;子链接和外键约束失败错误 - Entity framework parent -> child linking and foreign key constrain failed error 实体框架未将父ID映射为子表中的外键 - Entity Framework not mapping parent id as foreign key in child table 实体框架代码首先是多层父子外键 - Entity Framework code first multiple tier parent child foreign key 实体框架:WillCascadeOnDelete 和 Include - Entity Framework: WillCascadeOnDelete and Include 实体框架-当孩子有复合主键时为孩子父母-插入错误 - Entity Framework - Parent to Children When Child Has Composite Primary Key - Insert Error 实体框架级联删除 - FOREIGN KEY约束 - Entity Framework Cascade delete - FOREIGN KEY constraint 如何使用实体框架删除外键记录? - How to delete a foreign key record with Entity Framework? 实体框架6:删除多个父项的子项 - Entity Framework 6: Delete Child item for several parent 如何在ASP.NET MVC中使用实体框架在父表中创建外键并将其分配给子表 - How to create the foreign key in the parent table and assign it to the child table using entity framework in asp.net mvc 实体框架:如何在自引用父子关系上指定外键列的名称? - Entity Framework: How to specify the name of Foreign Key column on a self-referencing Parent-Child relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM