EF 4.0 - 使用条目的另一种方式

本文关键字:另一种 方式 EF | 更新日期: 2023-09-27 18:31:45

我想做这样的事情:

context.Entry(oldEntity).CurrentValues.SetValues(newEntity);

我使用 EF 4.0,所以我知道我可以使用context.ObjectStateManager来实现相同的目标

我看不到任何设置值的方法。我有ChangeObjectStateChangeRelationshipStateChangeRelationshipState功能可用。

事实上,我有这个错误:An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

所以我需要修改实体而不是附加它,以便解决我的问题。

EF 4.0 - 使用条目的另一种方式

试试这个:

objectStateManager.GetObjectStateEntry(oldEntity).ApplyCurrentValues(newEntity);