我在父>子关系中有两个表。
尝试删除父级时,出现以下错误:
The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.
我在 dbfirst 类型模型中使用 SQL Server 2012。
在数据库中,删除规则设置为级联,我仔细检查了 .edmx 中该规则也设置为级联。
我正在使用多个事务,然后在最后调用 .SaveChanges() ......不确定这是否与它有关。具体来说,我首先在另一个表上发布了一个更新,然后是这个删除。
这是我用于删除的通用存储库代码:
public virtual ActionConfirmation<int> Delete(TRepository entity, bool boolCommitChgs = true)
{
try
{
//DbSet.Remove(entity);
_dataContext.Entry(entity).State = System.Data.EntityState.Deleted;
if (boolCommitChgs)
{
_dataContext.SaveChanges();
}
return CRUDMessage(true, "deleted", entity);
}
catch (Exception ex)
{
return CRUDMessage(false, "delete", entity, ex);
}
}
然后我完成事务并在此处调用 SaveChanges,这是引发错误的时间:
try
{
dbContext.SaveChanges();
result = ActionConfirmation<int>.CreateSuccessConfirmation(
string.Format("{0} {1}: {2} successful.",
strNoun,
id,
strVerb
),
id
);
}
catch (Exception ex)
{
....etc....
任何想法为什么我会收到这个错误?