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
?
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