更新上下文建议 [英] Update context advices

查看:92
本文介绍了更新上下文建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我们的情况是我们使用"C#POCO实体生成器"生成了我们的实体。 (http://visualstudiogallery.msdn.microsoft.com/en-us/23df0450-5677-4926-96cc-173d02752313)。


我们通常做的事情(ASP.NET网站)是我们创建一个对象或从DB中获取它并将其存储在会话中。

然后我们使用该对象并更改其状态。

之后,我们想要存储如果是这样的话,DB中的新对象或更新我们提取的对象。


我试过没有成功完成这个并且想知道是否有人可以帮助我?也许目前的战略甚至不可能实现?使用下面的方法,我得到异常"具有相同密钥的对象已存在于
ObjectStateManager"中。我也试过ApplyCurrentValues(),但也在这里得到一个例外。如果我在会话中保存存储库(包含对象上下文),一切正常,但这根本不可取。


伪代码:


public void FetchObject(id){

   Session [" object"] = new Repository.GetByID(id);

}


public void ChangeObject(value){

   Session [" object"]。Attribute = value; = new Repository.GetByID(id);

}


public void SaveObject(){

  存储库rep = new Repository();  

   rep.Attach(会话[" object"]);

   rep.SaveChanges();

}


公共类存储库{

   public Repository(){

      this.Context = new ObjectContext();

   }
}


 


我非常感谢您的帮助!


 

解决方案

这里最好的选择是使用自我跟踪实体。当然,问题在于,由于您有一个普通的POCO对象,当您将它附加到存储库时,存储库不知道对象是如何更改的。如果您使用自我跟踪实体,那么
可以利用这些实体跟踪自己的更改这一事实,您可以使用ApplyChanges API将其应用于存储库。


当您查询对象时,您还应该使用NoTracking查询查询它们,以便它们不会附加到已连接到ObjectContext的对象上。


http://blogs.msdn.com/adonet/pages/feature -ctp-walkthrough-self-tracking-entities-for-entity-framework -asxx


 


 


Hi,

We have a situation where we have generated our entities using the "C# POCO Entity Generator" (http://visualstudiogallery.msdn.microsoft.com/en-us/23df0450-5677-4926-96cc-173d02752313).

What we usually do (ASP.NET website) is that we create an object or fetch it from DB and store it in session.
We then work with the object and change its state.
Afterwards, we would like to store the new object in DB or update the one we fetched if that was the case.

I have tried accomplishing this without any success and wonder if someone could help me out? Maybe it is not even possible with the current generation strategy? Using the approach below, I get the exception "object with the same key already exists in the ObjectStateManager". I have also tried ApplyCurrentValues(), but get an exception also here. Everything works fine if I save the repository (containing the object context) in session, but this is not desirable at all.

Pseudo-code:

public void FetchObject(id){
   Session["object"] = new Repository.GetByID(id);
}

public void ChangeObject(value){
   Session["object"].Attribute = value; = new Repository.GetByID(id);
}

public void SaveObject(){
   Repository rep = new Repository();  
   rep.Attach(Session["object"]);
   rep.SaveChanges();
}

public class Repository{
   public Repository(){
      this.Context = new ObjectContext();
   }
}

 

I'm very thankful for any help!

 

解决方案

Your best bet here is to use self-tracking entities. The issue, of course, is that since you have a plain POCO object, when you attach it to the repository, the repository has no idea how the object has changed. If you use self-tracking entities, you can make use of the fact that those entities track their own changes, which you can apply to the repository using the ApplyChanges API.

You should also, when you query objects out, want to query them with a NoTracking query so that they do not come to you already attached to an ObjectContext.

http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-self-tracking-entities-for-the-entity-framework.aspx

 

 


这篇关于更新上下文建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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