简体   繁体   English

实体框架级联删除不起作用

[英]Entity Framework Cascade Delete not working

I have two tables AgentTransmission and ClearinghousePartners that share a one to many relationship. 我有两个表AgentTransmissionClearinghousePartners ,它们共享一对多的关系。 Each AgentTransmission record can serve as a principal for many ClearinhouseParnters . 每个AgentTransmission记录都可以作为许多ClearinhouseParnters的主体。

This is represented via a List object in the AgentTransmission model. 这通过AgentTransmission模型中的List对象表示。

I've sent the OnDelete property to "Cascade" in the .edmx file, however when I attempt to delete an AgentTransmission object I'm getting this error. 我已将OnDelete属性发送到.edmx文件中的“Cascade”,但是当我尝试删除AgentTransmission对象时,我收到此错误。

The DELETE statement conflicted with the REFERENCE constraint
 \"FK_ClearinghousePartners_AgentTransmission\". The conflict
 occurred in database \"AgentResourcesU01\", table 
\"dbo.ClearinghousePartners\", column 'AgtTransId'.\r\nThe
 statement has been terminated.

AgentTransmission AgentTransmission

This contains a TON of fields so I've reduced it to only those that are relevant to the relationship 这包含一个TON字段,所以我把它减少到只与那些关系相关的字段

public partial class AgentTransmission
{
    public int ID { get; set; } //PK
    .
    .
    public virtual List<ClearinghousePartners> ClearinghousePartners { get; set; }
}

Clearinghouse Partners 信息交换机构合作伙伴

public partial class ClearinghousePartners
{
    public int Id { get; set; } 
    public string ClearingHouseName { get; set; }
    public string TradingPartnerName { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public int AgtTransId { get; set; } //FK

    public virtual AgentTransmission AgentTransmission { get; set; }
}

Controller 调节器

Nothing crazy here, just doing a normal delete. 这里没什么可疯狂的,只是正常删除。 Would like for the EF to delete all objects in the ClearinghousePartners List property of the AgentTransmission object.. which I believe it is capable of doing (although I could be wrong) 希望EF删除AgentTransmission对象的ClearinghousePartners List属性中的AgentTransmission对象..我相信它能够做到(虽然我可能是错的)

//Cascade deletes set to remove ClearinghousePartners
db.AgentTransmission.Remove(agenttransmission);
db.SaveChanges(); //Exception thrown here

EDMX EDMX

在此输入图像描述

The on delete setting needs to map to the database. on delete设置需要映射到数据库。 Have you updated the database from your edmx model? 您是否从edmx模型更新了数据库?

如果您使用的是数据库优先方法,则还需要确保在数据库中配置级联删除。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM