我正在为 EntityFramework 对象创建扩展,如 如何:自定义生成的数据中所述对象,但在其中一些扩展中,我需要获取实例的 ObjectContext 来查找模型中的一些其他值。我发现 提示 24 – 如何从实体获取 ObjectContext 但这是几年前写的,在此 类似的问题但我真的希望现在有更好的答案。
当然,这一定是经常需要的东西,以至于应该用官方方法支持从实体本身检索实体的对象上下文。
预先感谢有关此实施的任何最新信息。
I'm creating extensions for my EntityFramework objects as described in How to: Customize Generated Data Objects but in some of those extensions I need to get the instance's ObjectContext to look up some other values in the model. I've found Tip 24 – How to get the ObjectContext from an Entity but that was written a couple years ago, which is referenced in this similar SO question but I'm really hoping there's a better answer now.
Surely this must be something that's needed frequently enough that retrieval of an Entity's object context from the entity itself should be supported with an official method.
Thanks in advance for any more recent information on this implementation.
发布评论
评论(3)
还有另一种解决方案,使用连接属性。
使用连接的属性将如下所示(警告:未经测试的代码):
然后您可以使用Database1Entities.FromEntity从实体对象获取对象上下文。如果需要,您还可以在实体对象上定义实际属性:
在此解决方案中,实体对象上的
ObjectContext
属性是可选的。There is another solution, using connected properties.
Using connected properties would look like this (warning: untested code):
Then you can use
Database1Entities.FromEntity
to get the object context from an entity object. You can also define an actual property on the entity objects as well if you want:In this solution, the
ObjectContext
property on the entity objects is optional.不,没有任何这样的方法。所描述的解决方法看起来像是唯一的选择,因为该实体派生自
EntityObject
,其定义为:据我所知,只有
IEntityWithRelationships.RelationshipManager
会导致ObjectContext
>。这在 EF 4 中没有改变。而且从实体访问上下文并不常见。我可以想象,这对于在 EF 之上实现 Active Record 模式会很有帮助,但在这种情况下,您可能还可以控制在实体的静态方法内创建上下文,因此您应该能够将其设置为实体。在其他情况下,我会说这是你应该尽可能避免的事情。
No there is not any such method. The described workaround looks like the only option because the entity is derived from
EntityObject
which is defined as:As I know only
IEntityWithRelationships.RelationshipManager
leads toObjectContext
. This wasn't changed in EF 4.Also it is not really common to access the context from the entity. I can imagine that this can be helpful in case of implementing Active Record Pattern on top of EF but in such case you would also have probably control over creating the context inside the static method of the entity so you should be able to set it to the entity. In other cases I would say that it is something you should avoid as much as possible.
这就是我用的;这是一种基于约定的方法,可以轻松添加到项目中。
首先,向对象上下文添加挂钩:
这将尝试在与对象上下文关联的任何实体上动态设置名为
ObjectContext
的属性。接下来,将
ObjectContext
添加到实体类型:此解决方案确实需要将
ObjectContext
属性添加到每个实体类型。This is what I use; it's a convention-based approach that is simple to add to a project.
First, add hooks to your object context:
This will attempt to dynamically set a property called
ObjectContext
on any entity that is associated with the object context.Next, add an
ObjectContext
to the entity types:This solution does require an
ObjectContext
property to be added to each entity type.