首先更新实体EF代码时出错

时间:2017-01-30 11:07:40

标签: c# entity-framework ef-code-first code-first

我有以下架构:

Schema image

如何使用EF代码第一种方法更新实体?当我尝试这样做时:

this._context.Entry(foundEntity).CurrentValues.SetValues(entity);
this._context.Entry(foundEntity).State = EntityState.Modified;

抛出异常并说:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Bets_dbo.Matches_MatchID". The conflict occurred in database "UltraPlay.Betting.Platform", table "dbo.Matches", column 'ID'.
The statement has been terminated.

是否可以传递根对象和所有要更新的子项?如何干净,快速地做到这一点?

1 个答案:

答案 0 :(得分:0)

如果您想附加包含子元素的元素,您还需要附加所有子元素,例如

data

如果它已经存在。

否则EF假定子元素是模型的新元素并尝试(再次)插入它。

相关问题