为什么要使用DbContext而不是ObjectContext [英] Why to use DbContext and not ObjectContext
问题描述
现在我想创建我的POCO类。
为此,我已经从NuGet安装了EF 4.x DbContext Generator以及EF 4.x POCO实体生成器。
我完全意识到,它是建议使用DbContext为EF 4.1+,因为它是从ObjectContext派生的。但是,我仍然通过使用EF 4.x POCO实体生成器&然后也由EF 4.x DbContext生成器。
编辑:发现这个ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator(ObjectContext)
在任一情况下,它都会生成.Context.tt& Model.tt文件。那么任何一种工具生成的文件究竟有什么区别呢?为什么建议使用DbContext作为EF 4.1+?
我也想知道ObjectContext的限制。
DbContext是更新的API,它应该在使用大多数常见任务时抛光开发人员的体验 - 只要API设计更好,但是您仍然必须从 ObjectContext
code> DbContext ,如果要使用更复杂的功能,请使用较旧的API。如果您计划将EF升级到5.x或6.x,那么DbContext可能会更容易,因为这是ADO.NET团队的建议。
根据发电机EF 4.x POCO生成器创建更复杂的类,内部使用关系修复。这个功能证明自己在与懒惰加载一起使用时效率很低,所以更新的EF DbContext生成器不使用它。
旁注:从一个API到另外一个是完全支持的:
- 您可以使用
DbContext
构造函数接受ObjectContext
从ObjectContext API移动到DbContext API - 您可以使用
IObjectContext
适配器从DbContext API到ObjectContext API
I am using EF 4.1 & using DB first approach. I have created my .edmx file too. Now i wish to create my POCO classes.
For this, I have installed EF 4.x DbContext Generator as well as EF 4.x POCO Entity Generator from NuGet.
I am fully aware that it is recommnded to use DbContext for EF 4.1+ as it is derived from ObjectContext. However, i still generated my POCO classes by 1st using EF 4.x POCO Entity Generator & then also by EF 4.x DbContext Generator.
EDIT: Found this ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)
In either of the case, it generates .Context.tt & Model.tt files. So what is the exact difference in files generated by either of the tools ? Why is it recommnded to use DbContext for EF 4.1+ ?
I would also like to know limitations of ObjectContext.
DbContext is newer API which should polish developers experience when using most common tasks - simply the API is better designed but you still have to get ObjectContext
from DbContext
and use the older API if you want to use some more complex features. If you plan to upgrade EF to 5.x or 6.x in the future it will probably be easier with DbContext because that is what ADO.NET team is recommending.
In terms of generators EF 4.x POCO generator creates more complex classes which internally use relations fix up. This feature proved itself to be quite inefficient when used together with lazy loading so newer EF DbContext generator doesn't use it.
Side note: The code transition from one API to another is fully supported:
- You can use
DbContext
constructor acceptingObjectContext
to move from ObjectContext API to DbContext API - You can use
IObjectContext
adapter to move from DbContext API to ObjectContext API
这篇关于为什么要使用DbContext而不是ObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!