Reputation: 2449
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:
Upvotes: 1
Views: 1511
Reputation: 2135
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)
or the better solution is to use
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
Upvotes: 0
Reputation: 13146
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;
Upvotes: 1