【问题标题】:EF error on updating entity/model :The UPDATE statement conflicted with the FOREIGN KEY constraint更新实体/模型时出现 EF 错误:UPDATE 语句与 FOREIGN KEY 约束冲突
【发布时间】:2017-11-30 18:23:58
【问题描述】:

我有一个名为RFM_NOTF_Notifications 的表,它引用了另一个表RFM_NOTD_NotificationsDetail。我正在使用 EF 与数据库通信,NotificationsDetail 实体有一个Notifications 列表。

当我尝试更新Notifications 实体列表时,我收到了这个错误:

UPDATE 语句与 FOREIGN KEY 约束“FK_RFM_NOTF_Notifications_RFM_NOTD_NotificationsDetail”冲突。冲突发生在数据库“RegulatoryFileManagementScrumQA”、表“dbo.RFM_NOTD_NotificationsDetail”、列“RFM_NOTD_P_NotificationDetailID”中

这是存储库中的添加/更新方法:

private void CreateNotifications(GeneralBO file, RFM_NOTD_NotificationsDetail notfFile)
{
        foreach (NotificationsBO notf in file.Notifications)
        {
            RFM_NOTF_Notifications nt = new RFM_NOTF_Notifications
            {
                RFM_NOTF_B_Description = notf.Description
            };

            if(notf.Id > 0)   // Save
            {
                nt.RFM_NOTF_P_NotificationID = notf.Id;
                nt.RFM_NOTD_F_NotificationDetailID = file.Id;

                nt.RFM_NOTF_M_ModifiedById = file.CreatedById;
                nt.RFM_NOTF_M_ModifiedDateTime = DateTime.Now;
                new GenericRepository<RFM_NOTF_Notifications>(_dbContext).update(nt);                    
            }
            else
            {
                nt.RFM_NOTF_M_CreatedById = file.CreatedById;
                nt.RFM_NOTF_M_CreatedDateTime = DateTime.Now;
                notfFile.RFM_NOTF_Notifications.Add(nt);
            }
        }
}

GeneralBO 是保存 UI 数据的模型/对象。

这些是业务对象/模型

public class NotificationsBO 
{
    public long Id { get; set; }
    public string Description { get; set; }
    public int CreatedById { get; set; }
    public DateTime CreatedDateTime { get; set; }
    public int ModifiedById { get; set; }
    public DateTime ModifiedDateTime { get; set; }
}

public class NotificationDetailsBO : ApplicationBO
{
    public long Id { get; set; }
    .......
    .......
    public List<NotificationsBO> Notifications { get; set; }
}

我的桌子是:

【问题讨论】:

    标签: c# .net sql-server entity-framework foreign-keys


    【解决方案1】:

    我怀疑nt.RFM_NOTD_F_NotificationDetailID = file.Id; 我认为NotificationDetails 表中没有file.Id 的记录。

    有件事引起了我的注意,有一个参数被传递给名为RFM_NOTD_NotificationsDetail notfFile 的方法。我不知道数据的来源和目的,但你会这样尝试吗?

    nt.RFM_NOTD_F_NotificationDetailID = notfFile.Id;
    

    【讨论】:

      【解决方案2】:

      让我先解释一下这个错误是什么意思。

      Update 语句与外键冲突

      就是说Notifications表的主键是Notification Detail表的外键,所以不能改,明显是冲突。

      您可以尝试以下几种解决方案。 (不推荐)

      • 先删除通知详细信息,然后再删除通知

      或者更好的解决方案是使用

      • Fluent API 实现级联删除,一旦您从 Notifications Table 中删除数据,就会从 Notifications Detail 表中删除值

      你可以在数据库中使用

      ALTER TABLE TableName
      ADD CONSTRAINT [New_FK_Constraint]
      FOREIGN KEY (ColumnName) REFERENCES SecondTable(ColumnName)
      ON DELETE CASCADE ON UPDATE CASCADE
      GO 
      

      AND 在更新的情况下。

      您需要使用 Entity Framework 的 EntityStateModified 属性来更好地实现更新功能

      【讨论】:

        猜你喜欢
        • 2014-07-14
        • 1970-01-01
        • 2023-03-06
        • 2019-05-09
        • 1970-01-01
        • 1970-01-01
        • 2018-10-12
        相关资源
        最近更新 更多