实体框架4:访问部分实体类中的当前数据上下文

发布于 2024-10-20 13:04:26 字数 127 浏览 3 评论 0原文

我想使用方法和属性扩展分部类中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2024-10-27 13:04:26

没有构建方法可以从实体获取当前的ObjectContext。基于 EntityObject 类和 POCO 代理的实体在内部使用 ObjecContext,但它们不公开它。

将这种依赖性添加到您的实体中被认为是糟糕的设计,因此您也许应该解释一下您正在尝试做什么,我们可以找到其他(更好)的解决方案。

There is no build in way to get current ObjectContext from entity. Entities based on EntityObject class and POCO proxies uses ObjecContext 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.

GRAY°灰色天空 2024-10-27 13:04:26

尽管不推荐它,而且我自己也不使用它(正如拉迪斯拉夫所说:糟糕的设计),我偶然发现了一个解决方案:

http://blogs.msdn.com/b/alexj/archive /2009/06/08/tip-24-how-to-get-the-objectcontext-from-an-entity.aspx

扩展方法:

public static ObjectContext GetContext( 
   this IEntityWithRelationships entity 
) 
{ 
    if (entity == null)  
       throw new ArgumentNullException("entity"); 

    var relationshipManager = entity.RelationshipManager; 

    var relatedEnd = relationshipManager.GetAllRelatedEnds() 
                                        .FirstOrDefault(); 

    if (relatedEnd == null)  
       throw new Exception("No relationships found"); 

    var query = relatedEnd.CreateSourceQuery() as ObjectQuery; 

    if (query == null)  
       throw new Exception("The Entity is Detached"); 

    return query.Context; 
}

在实体内

var myContext = this.GetContext() as MyEntities;

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:

public static ObjectContext GetContext( 
   this IEntityWithRelationships entity 
) 
{ 
    if (entity == null)  
       throw new ArgumentNullException("entity"); 

    var relationshipManager = entity.RelationshipManager; 

    var relatedEnd = relationshipManager.GetAllRelatedEnds() 
                                        .FirstOrDefault(); 

    if (relatedEnd == null)  
       throw new Exception("No relationships found"); 

    var query = relatedEnd.CreateSourceQuery() as ObjectQuery; 

    if (query == null)  
       throw new Exception("The Entity is Detached"); 

    return query.Context; 
}

within the entity

var myContext = this.GetContext() as MyEntities;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文