2013-07-07 29 views
0

在我的数据库中,我有一列定义为唯一索引。我从DB生成了我的EF模型(我不知道这个唯一性是如何被转换为模型的,我该如何检查它?)。
当我尝试使用更新唯一的非主键

entity.Users.ApplyCurrentValues(user); 

我碰到下面的错误记录来更新这个字段:

An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied.

如何更新独特的(非主!)场?

+0

对于查看您的用户模型非常有用。 – dbugger

回答

0

我们不得不看一些更多的代码来确定你从哪里获得user对象,以便更清楚地了解发生了什么。

我相信ApplyCurrentValues用于更新已经存在于ObjectContext中的对象所做的更改;如果您最初没有从上下文中获取您的user实例,那么您将得到上述错误(错误是告诉您无法找到您要更新的对象)。

MSDNCopies the scalar values from the supplied object into the object in the ObjectContext that has the same key.

很可能是你能Attach()user如果它是一个分离的对象,或者您可以在您的user从这种情况下加载,然后使用ApplyCurrentValues与检索到的对象。