简体   繁体   English

实体框架4-在生成的实体中嵌入ObjectContext引用

[英]Entity Framework 4 - Embed ObjectContext reference within generated Entities

I am new to Entity Framework 4.0, using it with C#, and currently experimenting with its features. 我是Entity Framework 4.0的新手,将其与C#结合使用,目前正在尝试使用其功能。

What I noticed is that, like with most similar ORMs, it relies on an Context object to deal with the data-manipulation and CRUD statements generation done on the generated entities. 我注意到的是,与大多数类似的ORM一样,它依赖于Context对象来处理在生成的实体上完成的数据操作和CRUD语句。

This means that if I want to save the changes back to the database, I always need to be able to have access to a reference to the ObjectContext that has instanciated the entities. 这意味着,如果我要将更改保存回数据库,则始终需要能够访问对实例化了实体的ObjectContext的引用。

It is fine and all if the context has been created in an accessable scope (same method, for example), but what if I pass an entity or and entity set to a method and want this method to save the changes? 很好,如果上下文是在可访问的范围内创建的(例如,相同的方法),但是如果我将一个实体或和实体集传递给一个方法并希望此方法保存更改怎么办? It looks like the only easy way is to pass the ObjectContext along with the parameters. 看起来唯一简单的方法是将ObjectContext与参数一起传递。

Another solution would be placing the ObjectContext in some sort of global variable. 另一种解决方案是将ObjectContext放在某种全局变量中。 Needless to say, I find styling and maintainability issues to both of these approaches. 不用说,我发现这两种方法都存在样式和可维护性问题。

In short, the best way I can imagine is getting a reference to the ObjectContext from the entity or entity set. 简而言之,我能想象的最好方法是从实体或实体集中获取对ObjectContext的引用。 I know that it isn't possible by default. 我知道默认情况下是不可能的。

I have found a method showing adding an extension method to get the ObjectContext from the entity. 我发现了一种显示添加扩展方法以从实体获取ObjectContext的方法。 However, it only works for entities with relationships and calling this method is expensive according to the author. 但是,它仅适用于具有关系的实体,根据作者的说法,调用此方法很昂贵。

I was thinking about maybe modifying the T4 template to add a Context property to all my entities and fill it automatically on entities' instanciation. 我当时正在考虑修改T4模板以向所有实体添加Context属性,并根据实体的实例自动填充它。

I have already modified T4 template once to have Entity Framework enforce Max Length on my generated classes (by following Julie Lerman's Programming Entity Framework 4 book). 我已经修改过T4模板一次,以使Entity Framework在生成的类上强制执行Max Length(通过遵循Julie Lerman的《 Programming Entity Framework 4》一书)。 I can't say I really enjoy the T4 syntax so far, but if that's the best/only way, so be it... 我不能说到目前为止我真的很喜欢T4语法,但是如果那是最好/唯一的方法,那就去吧...

Has anyone already done so and what would be the best way to handle this and willing to share his T4 template or explain what are the best partial methods or events to hook into to get this done? 是否有人已经这样做了?什么是处理此问题的最佳方法,并愿意分享他的T4模板或解释完成该工作的最佳部分方法或事件?

Is there any major downside in using such an approach? 使用这种方法有什么重大缺点吗? I am thinking that having so many references to the ObjectContext may hinder/delay its ability to be recollected by the GC if some of my entities remain in scope but I actually have no use anymore for the ObjectContext. 我认为,如果我的某些实体仍然在作用域中,那么对ObjectContext的引用过多可能会阻止/延迟其被GC重新收集的能力,但实际上我对ObjectContext不再使用了。

Many thanks. 非常感谢。

If you need to pass object context as parameter together with your entities you are doing something wrong. 如果您需要将对象上下文作为参数传递给您的实体,那么您做错了什么。

Usually context is needed only in well defined layer. 通常,仅在定义明确的层中才需要上下文。 All classes from this layer which requires context to their logic can receive the context through some specialized class - context provider (it can also be called service locator). 该层中所有需要上下文逻辑的类都可以通过某些专门的类-上下文提供程序(也可以称为服务定位器)来接收上下文。 Context provider will hold current context instance in some storage - you can create your own or you can store it per thread, per http request, etc. 上下文提供程序会将当前上下文实例保存在某些存储中-您可以创建自己的上下文实例,也可以按线程,按http请求等将其存储。

If they need more then one context instance in your classes you can modify your provider to work also as factory. 如果他们在您的类中需要一个以上的上下文实例,则可以修改提供程序以使其也可以作为工厂工作。

Another common approach is combined with dependency injection. 另一种常见方法是与依赖项注入结合使用。 You will pass the context to your classes through the constructor (or property) and you will have some bootstraper code which will do all necessary initialization for you (create required instances and pass all dependencies into them). 您将通过构造函数(或属性)将上下文传递给类,并且您将拥有一些引导程序代码,这些代码将为您执行所有必要的初始化(创建所需的实例并将所有依赖项传递给它们)。 Again you can pass a context or a factory. 同样,您可以传递上下文或工厂。 This is usually used together with IoC containers which will do the plumbing for you. 通常将其与IoC容器一起使用,这些容器将为您提供水暖服务。

Once you have this infrastructure prepared you can pass your entity to the any initialized class from that layer and it will have the context available. 准备好此基础结构后,您可以将您的实体传递给该层中的任何初始化的类,并且它将具有可用的上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM