问题描述
我使用 ObjectContexts
很长时间了。现在,我已经安装了VS 2012,令我惊讶的是,实体数据模型没有用于生成 ObjectContexts
和 EntityObjects的代码生成项的选项
而不是 DbContexts
和 POCO
。
I've been using ObjectContexts
for quite a long time now. Now that I've installed VS 2012, for my surprise the Entity Data Model does not have an option for a Code Generation Item that creates ObjectContexts
and EntityObjects
instead of DbContexts
and POCOs
.
现在不推荐使用吗?我看到 ObjectContext
类仍然可用,但无法将其用作我的EDMX的输出。
Is it deprecated now? I see the ObjectContext
class is still available, but there's no way I can use it as the output of my EDMXs.
推荐答案
不推荐使用ObjectContext。实际上,DbContext只是ObjectContext之上的一层。 DbContext API具有更好/更小的API,并且更易于使用。 POCO实体也更清洁。这就是更改代码生成的原因。如果仍要使用基于ObjectContext和EntityObject的实体,请删除* .edmx文件下的* .tt文件。双击edmx文件在设计器中将其打开。单击设计器表面。在属性中,将代码生成策略从无更改为默认。
ObjectContext is not deprecated. Actually DbContext is just a layer on top of ObjectContext. DbContext API has much nicer/smaller API and is easier to work with. Also POCO entities are much cleaner. This was the reason to change code generation. If you still want to use ObjectContext and EntityObject based entities remove *.tt files under the *.edmx file. Double click the edmx file to open it in the designer. Click on the designer surface. In the properties change "Code Generation Strategy" from "None" to "Default".
EDIT
在VS2013附带的EF6设计器中,此行为有所改变(可从VS2012下载)。如果您使用的是EF5,则两个选项为: T4和 LegacyObjectContext。如果您使用的是EF6,则下拉菜单将被禁用。如果您想在EF6应用中使用ObjectContext,则需要使用(请注意,此链接仅指向模板的一个版本,但实际上有几个版本取决于您的需要-C#与VB.NET,非WebSite WebSite-是列表)。您可以在EF6工具中找到有关更改的更多详细信息。
The behavior has changed a bit in the EF6 designer that ships with VS2013 (and is available for download for VS2012). If you are using EF5 the two options are: 'T4' and 'LegacyObjectContext'. If you are using EF6 the dropdown is disabled. If you would like to use ObjectContext in an EF6 app you need to use Entity Object generator for EF6 (note this link points just to one version of the template but there are actually a few versions depending on what you need - C# vs. VB.NET, WebSite non-WebSite - here is the list). You can find more details about changes in the EF6 tooling here.
这篇关于.NET 4.5中不推荐使用ObjectContext吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!