EntityFramework丢失了对象 [英] EntityFramework looses objects

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

问题描述

嘿伙计们,



我现在正在使用Entity Framework 5并尝试构建我的第一个数据库应用程序。我有2个表:



DataSet1和DataSet2。



DataSet1引用DataSet2和由EntityFramework构建的代码以某种方式工作,如果我从DB读取DataSet1,DataSet2已作为DataSet1中的对象包含在内,而不仅仅是键(ID),而是整个对象。这基本上是好的,因为我喜欢直接访问该对象。所以我不需要回到数据库来搜索正确的对象。



虽然我一直都可以访问DataSet1但似乎我松了DataSet2不时。我不能说什么时候或为什么,但有时候我可以通过DataSet1对象访问DataSet2,有时我得到例外:



'(m.projectset).Client '抛出了'System.ObjectDisposedException'类型的异常



是否有规则,为什么我会失去联系或如何避免这种情况?

如何处理这样的关系,如果一个对象包含另一个对象而不仅仅是它的关键字?



最好

J

Hey guys,

I am working with the Entity Framework 5 at the moment and try to build my first DB application. I do have 2 tables:

DataSet1 and DataSet2.

DataSet1 has a reference to DataSet2 and the code that gets build by the EntityFramework works in a way, that if I read DataSet1 from the DB, DataSet2 is already included as an object in DataSet1, not just the key (ID), but the whole object. Which is basically good, because I like to have the direct access to the object. So I do not need to get back to the DB to search for the right object.

While I do have access to DataSet1 all the time, it seems that I loose DataSet2 from time to time. I cannot say when or why, but sometimes I do have access to DataSet2 via the DataSet1 object and sometimes I get the Exception:

'(m.projectset).Client' threw an exception of type 'System.ObjectDisposedException'

Is there a rule, why I lose connection or how to avoid this?
how to deal with such relations, if one object contains another object instead of just the key of it?

Best
J

推荐答案

解决了它。渴望加载是解决方案!
Solved it. Eager loading was the solution!


这篇关于EntityFramework丢失了对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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