简体   繁体   中英

EF error on updating entity/model :The UPDATE statement conflicted with the FOREIGN KEY constraint

I have a table named RFM_NOTF_Notifications which has a reference to another table RFM_NOTD_NotificationsDetail . I'm using EF to communicate with database and NotificationsDetail entity has a list of Notifications .

When I tried to update a list of Notifications entity, I'm getting this error:

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_RFM_NOTF_Notifications_RFM_NOTD_NotificationsDetail". The conflict occurred in database "RegulatoryFileManagementScrumQA", table "dbo.RFM_NOTD_NotificationsDetail", column 'RFM_NOTD_P_NotificationDetailID'

And this is the add/update method inside repository:

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 is the model/object which hold data from UI.

And these are the business objects/model

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; }
}

And my tables are:

在此处输入图片说明

I suspect that nt.RFM_NOTD_F_NotificationDetailID = file.Id; I think there is no record in NotificationDetails table with file.Id .

Something caught my attention, there is a parameter which are passed the method with named RFM_NOTD_NotificationsDetail notfFile . I don't know that data where it is being came from and it's purpose but would you try like this;

nt.RFM_NOTD_F_NotificationDetailID = notfFile.Id;

Let me first explain what does that error means.

The Update statement conflicted with the Foreign Key

It simply means that the primary key of table Notifications is a foreign key in the Notification Detail table so you can't change it as it is a conflict clearly.

Here are a few solutions that you can try. (not recommended)

  • Remove the Notification Details first and then Remove Notifications

or the better solution is to use

  • Fluent API to implement cascade delete which will delete the values from Notifications Detail table as soon as you will delete the data from Notifications Table

and you can achieve the same in the database using

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

AND in case of an update.

You need to use the EntityStateModified property of Entity Framework to better implement the update feature

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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