(注意:这不是这个问题的重复,即使它有相同的例外。)
我有一个穷人的交易,策略是:
- 插入父子记录。
- 执行长时间运行的操作。
- 如果长时间运行失败,去删除之前插入的父子记录。
当我尝试第 3 步时,我收到以下消息:
操作失败:无法更改关系,因为一个或多个外键属性不可为空。当对关系进行更改时,相关的外键属性将设置为空值。如果外键不支持空值,则必须定义新关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。
我大体上理解这意味着什么,但我认为我是在遵守规则,无论我多么努力地遵守规则,我都不确定为什么会收到这条消息。
我们使用自我跟踪实体,我的代码实际上是这样的:
var parent = new Parent(1,2,3);
var child = new Child(4,5,6);
parent.Children.Add(child);
MyContext.Parents.ApplyChanges(parent);
MyContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
// At this point, inserts were successful and entities are in an Unchanged state.
// Also at this point, I see that parent.Children.Count == 1
var shouldDeleteEntities = false;
try
{
// This is not database-related. This process does some
// encryption/decryption and uploads some files up to
// Azure blob storage. It doesn't touch the DB.
SomeLongRunningProcess();
}
catch
{
// Oops, something bad happened. Let's delete the entities!
shouldDeleteEntities = true;
}
// At this point, both entities are in an Unchanged state, child still
// appears in parent.Children, nothing is wrong that I can see.
parent.MarkAsDeleted();
child.MarkAsDeleted();
// I've tried MyContext.ApplyChanges here for both entities, no change.
// At this point, everything appears to be in the state that
// they're supposed to be!
try
{
MyContext.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
catch
{
// This exception was thrown and I can't figure out why!
}
这个逻辑有什么问题?为什么我不能简单地删除这两条记录?MyContext.ApplyChanges
我打电话后试过打电话MarkAsDeleted
。我已经尝试了各种各样的事情,无论如何,无论我多么努力地告诉上下文我想要删除它们,它都会不断抛出这个异常。