Is ObjectContext deprecated in .NET 4.5?

后端 未结 3 2037
北海茫月
北海茫月 2021-02-05 14:36

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 Cod

相关标签:
3条回答
  • 2021-02-05 14:48

    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

    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.

    0 讨论(0)
  • 2021-02-05 14:49

    Microsoft recommends the EF5.x DbContext Generator for new projects. It is the default template in VS2012. PreVS2012 users can find it using the ExtensionManager (search for EF5.x) or download it here.

    You can of course still generate EntityObjects and ObjectContext by using the EF4.x EntityObject Generatortemplate which can be found here.

    How to set things up is described here.

    0 讨论(0)
  • 2021-02-05 15:06

    I'm not sure how to make an EDMX generate using ObjectContext, but no it is not deprecated. DbContext is used because for 99% of users, the functionality of ObjectContext is not needed.

    You can get a reference to an ObjectContext from a DbContext, though: ((IObjectContextAdapter)dbContext).ObjectContext

    0 讨论(0)
提交回复
热议问题