当实体框架上下文超出范围时会发生什么? [英] What happens when an Entity Framework context goes out of scope?

查看:69
本文介绍了当实体框架上下文超出范围时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经困扰了我一段时间,不知道它是如何工作的.

使用Entity Framework时,假设您创建上下文的实例,在该实例上运行查询,从查询中获取一个对象,将该对象传递到其他地方,然后使上下文超出范围.

检索到的对象会怎样?它会变得分离吗?如果我修改它并想写回数据库怎么办?

这是一个示例代码片段,用于解释我的意思:

(假设MyObject是由实体框架设计者创建的代表特定数据库表的类).

This has been bugging me for a while, not sure how it works.

When working with an Entity Framework, suppose you create an instance of your context, run a query on it, grab an object from the query, pass that object somewhere else, then let the context go out of scope.

What happens to the retrieved object? Does it become detached? What if I modify it and want to write back to DB?

Here''s a sample snippet code to explain what I mean:

(suppose MyObject is a class created by the Entity Framework designer to represent a particular DB table)

public class foo
{
   public MyObject currentMyObject;

   public void GetMyObject()
   {
       using (MyEFEntitiescontext = new MyEFEntities())
       {
            currentMyObject = context.MyObjects.FirstOrDefault();
       }
   }

   public void doSomething()
   {
      //Do some operations on the currentMyObject field

      //Save the currentMyObject back to the DB
   }
}


(注意-我只是将代码放在这里,而不是在VS中,所以请忽略语法错误:))

如果我先运行GetMyObject(),然后再运行函数doSomething(),那么该函数内部会看到什么?它如何将对当前对象所做的更改发送回数据库?

谢谢,
Juliean.


(note - I just threw the code together here, not in VS, so please ignore syntax errors :) )

If I run GetMyObject() first, and then run the function doSomething() second, what does the inside of that function see? How can it send changes made to the current object back to the DB?

Thanks,
Juliean.

推荐答案

巧合的是,我今天问自己也是一样!
我发现该对象已分离.因此,将任何属性更改为EntityObject都不会再更改其状态.
由于它是调用SaveChanges的Context,因此您也永远无法将EntityObject保存回数据库. Context.StateManager可能会更改EntityObject的状态,但这也已被Dispose off.因此,您实际上拥有一个Untracked EntityObject. Untracked实体对于绑定到想要选择一个值但从不更改它的值的查找(如ComboBoxes)特别有用.
如果有可能将EntityObject添加到另一个我不知道的上下文中(但我不这样认为).但是我认为您可以足够轻松地测试自己:)
Coincidentally I asked myself the same today!
What I found is that the object becomes detached. So changing any Properties to the EntityObject does not change its state anymore.
Since its the Context where you call the SaveChanges you would also never be able to save the EntityObject back to the database. The Context.StateManager could possibly alter the state of your EntityObject, but that is also Disposed off. So you effectively have an Untracked EntityObject. Untracked Entities are especially useful for binding to lookups like ComboBoxes where you want to select a value, but never change it.
If it would be possible to add the EntityObject to another Context I do not know (but I do not think so). But I think you could test that yourself easily enough :)


如果Context超出范围,我认为您无法执行SaveChange(),因为上下文是知道如何执行操作的人以及上下文还可以跟踪对象的状态.
If Context goes out of scope, I think you cannot do the SaveChange() because The context is the one who know how to do that and the context also keep track about the state of objects.


这篇关于当实体框架上下文超出范围时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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