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