【发布时间】: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