调用ObjectContext.ApplylCurrentValues< T>(字符串,T)的异常 [英] Exception calling ObjectContext.ApplylCurrentValues<T>(string, T)

查看:102
本文介绍了调用ObjectContext.ApplylCurrentValues< T>(字符串,T)的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用带有POCO的EF4。


执行以下代码时,我得到以下例外:


User user = UnitOfWork.Model.Users.Single(u => u.UserGuid == theRequest.UserGuid);
user.IsApproved = true;
user.IsLockedOut = false;
UnitOfWork.Model.ApplyCurrentValues(" Users",user);


具有键的对象匹配在ObjectStateManager中找不到提供的对象的键。验证提供的对象的键值是否与必须应用更改的对象的键值匹配。


对于我做错了什么的任何想法?如您所见,我从ObjectContext获取User实例。


Thanks


解决方案

你好Devlife3,
http:// msdn。 microsoft.com/en-us/library/dd487246(VS.100).aspx ),NBSP;提供的对象的实体键必须与ObjectStateManager中的条目的EntityKey属性匹配。换句话说,该方法用于改变已经跟踪的实体的当前值。

在你的代码中,有几个我看不到的细节,但似乎是不正确的:

首先,我不知道你的其他地方是否有您正在将Users.MergeOption设置为NoTracking。如果这样做,则不会跟踪查询的结果。

其次,我不知道UnitOfWork.Model是否会在你使用它时两次返回相同的ObjectContext实例。如果是,则不需要调用ApplyCurrentValues,因为正在跟踪User的实例,所有更改将在SaveChanges之前被动态检测到。如果UnitOfWork返回两个不同的实例,它被引用两次,那么对ApplyCurrentValues的调用可能会收到第二个ObjectContext未知的User实例,因此预计会收到此异常。请告诉我这是什么情况。

感谢,



I'm using EF4 with POCO.

When executing the following code I get the following exception:

User user = UnitOfWork.Model.Users.Single( u => u.UserGuid == theRequest.UserGuid );
user.IsApproved = true;
user.IsLockedOut = false;
UnitOfWork.Model.ApplyCurrentValues( "Users", 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.

Any thoughts as to what I'm doing wrong? As you can see I'm getting the User instance from the ObjectContext.

Thanks

解决方案

Hi Devlife3,

As you will see in the documentation of this method (http://msdn.microsoft.com/en-us/library/dd487246(VS.100).aspx),  the entity key of the supplied object must match the EntityKey property of an entry in the ObjectStateManager. In other words, the method is used to change the current values of an entity that is already tracked.

In you code, there are several details that I cannot see, but it seems to be incorrect:

First of all, I don't know if somewhere else in your code you are are setting Users.MergeOption to NoTracking. If you do, then results from your query are not going to be tracked.

Second, I don't know if UnitOfWork.Model will return the same instance of ObjectContext the two times you use it. If it does, then there should be no need to call ApplyCurrentValues, since the instance of User is being tracked all changes will be detected actumatically before SaveChanges. If UnitOfWork returns two different instances the two times it is referenced, then the call to ApplyCurrentValues will likely receive a User instance that is unknown for the second ObjectContext, and therefore it is expected that you receive this exception.

Please, let me know what is thecase.

Thanks,
Diego


这篇关于调用ObjectContext.ApplylCurrentValues< T>(字符串,T)的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆