简体   繁体   English

更新唯一的非主键

[英]Updating unique non-primary key

In my DB, I have a column defined as a Unique index. 在我的数据库中,我有一列定义为唯一索引。 I generated my EF model from the DB (I don't know how this uniqueness was translated to the model. How do I check this?). 我从数据库生成了EF模型(我不知道如何将这种唯一性转换为模型。如何检查?)。
When I try to update this field in a record using 当我尝试使用以下方式更新记录中的此字段时

entity.Users.ApplyCurrentValues(user);

I get the following error: 我收到以下错误:

An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. 在ObjectStateManager中找不到具有与提供的对象的键匹配的键的对象。 Verify that the key values of the supplied object match the key values of the object to which changes must be applied. 验证提供的对象的键值与必须应用更改的对象的键值匹配。

How can I update a unique (non-primary!) field? 如何更新唯一(非主要!)字段?

We'd have to see some more code to determine where you're obtaining user object from to get a clearer picture of what's going on. 我们必须查看更多代码来确定您从何处获取user对象,以更清楚地了解正在发生的事情。

I believe ApplyCurrentValues is used to update changes that have been made to an object that already exists in the ObjectContext ; 我相信ApplyCurrentValues用于更新对ObjectContext中已经存在的对象所做的更改; if you didn't initially obtain your user instance from your context, you'll get the error above (the error is telling you it's unable to find the object you're trying to update). 如果您最初没有从上下文中获取user实例,则会收到上面的错误(该错误表明您无法找到要更新的对象)。

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

You may well be able to Attach() your user if it's a detached object, or you may be able to load your user from this context and then use ApplyCurrentValues with the retrieved object. 如果它是一个分离的对象,您可能可以Attach()您的user ,或者您可以从此上下文中加载user ,然后对检索到的对象使用ApplyCurrentValues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM