Get DbContext for Entities

梦想与她 提交于 2019-12-08 04:34:19

问题


Okay, I feel a bit foolish for having to ask this but I guess my understanding of the inner workings of Entity Framework is lacking.

I'd like to experiment with work with DbContext. I have an existing ASP.NET MVC application using EF 4.2. I can get my entities using:

var context = new MyEntities();

And this works just fine.

But how the heck to I get the same data represented by a DbContext?


回答1:


So I guess you are using default code generator provided by EDMX designer - it will use ObjectContext and heavy weight EntityObject based entities.

If you want to use DbContext you must:

  • Turn off that default code generation - in property window remove Custom Tool for EDMX file
  • Download and install DbContext T4 generator (you can get it directly from extension manager in Visual Studio)
  • In EF designer select Add Code Generation Item from context menu in the designer surface (not on entity)

Now EF will add two .tt files to your project - one will be responsible for creating a new class for every entity or complex type defined in your EDMX file and the second will be responsible for creating class derived from DbContext and exposing sets for all your entity types



来源:https://stackoverflow.com/questions/11180250/get-dbcontext-for-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!