实体框架4:访问部分实体类中的当前数据上下文
我想使用方法和属性扩展分部类中的 EF 实体。我经常这样做。 但现在我需要将该实体的数据与其他实体的数据结合起来。因此,我需要能够访问实体对象上下文(如果已附加)来进行这些查询。 有没有办法从其中获取实体对象上下文?
谢谢!
I want to extend an EF entity in a partial class with methods and properties. I've done this quite often.
But now I would need to combine data from this entity with data from other entities. I would therefore need to able to access the entities objectcontext (if attached) to make these queries.
Is there a way to get the entities objectcontext from within it?
Thanx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有构建方法可以从实体获取当前的
ObjectContext
。基于EntityObject
类和 POCO 代理的实体在内部使用ObjecContext
,但它们不公开它。将这种依赖性添加到您的实体中被认为是糟糕的设计,因此您也许应该解释一下您正在尝试做什么,我们可以找到其他(更好)的解决方案。
There is no build in way to get current
ObjectContext
from entity. Entities based onEntityObject
class and POCO proxies usesObjecContext
internally but they don't expose it.Adding such depnedency into your entities is considered as bad design so you should perhaps explain what you are trying to do and we can find other (better) solution.
尽管不推荐它,而且我自己也不使用它(正如拉迪斯拉夫所说:糟糕的设计),我偶然发现了一个解决方案:
http://blogs.msdn.com/b/alexj/archive /2009/06/08/tip-24-how-to-get-the-objectcontext-from-an-entity.aspx
扩展方法:
在实体内
Even though it is not recommended, and I myself don't use it (as Ladislav stated: bad design), I stumbled upon a solution:
http://blogs.msdn.com/b/alexj/archive/2009/06/08/tip-24-how-to-get-the-objectcontext-from-an-entity.aspx
Extension Method:
within the entity