How do I stop Entity Framework from generating BOTH ObjectContext and dbContext
Asked Answered
O

4

13

Background

  • Visual Studio 2012
  • NEW Model.EDMX file created in VS2012
  • Copied some of the EDMX xml from a previously created EDMX into the new one

Problem / Question

Now. The EDMX (TT transform, custom tool, whatever, etc.) is generating BOTH DbContext classes (under the Model.tt/Model.Context.tt files) and ObjectContext classes (via the Model.designer.cs file). See image below:

Both the Model.tt AND the Designer.cs files are defining my entities

Everything builds fine and works with the DbContext (but obviously only if I delete the Designer.cs file just before building) but the Designer.cs - and its ObjectContext-based code - keeps reappearing. How do I stop this behavior?!

Overgrowth answered 25/10, 2012 at 18:0 Comment(2)
Have you tried clicking on the designer surface and changing Code Generation Strategy to None?Cinemascope
But I do want to generate code... DbContext code via the Model.tt and the Model.Context.tt files; I just don't want the old-school ObjectContext from Model.Desiger.csOvergrowth
O
8

I didn't find out how to stop the Designer.cs file from generating the ObjectContext, but I did figure out how to make it so that it doesn't matter. Just set the Build Action to "None" instead of "Compile".

Set the Build Action to None

Overgrowth answered 25/10, 2012 at 18:17 Comment(0)
U
3

The .tt files will generate the code regardless of the code generation strategy in the .edmx. They listen to the .edmx file changes. At least this is how they are working for me.

So by turning the code generation strategy to None in the .edmx you make the .designer.cs file empty of any useful content.

Then open up the project file, find the nodes representing the .edmx, by default it is contained in EntityDeploy node ie the Build Action value, and remove its Generator subkey.

Uninspired answered 13/3, 2013 at 6:55 Comment(0)
C
1

On normal EF Code First projects, Code Generation Strategy is "None" and ObjectContext is not generated.

It appears in your case that you Code Generation Strategy set to "Default".

To stop generating ObjectContext in xxxx.Designer.cs, go to your edmx file, and change your Code Generation Strategy from "Default" to "None".

If you inspect xxxx.Designer.cs

// Default code generation is disabled for model 'C:\Users\xyxy\xyxyxy.Web\Models\xyxy.edmx'. 
// To enable default code generation, change the value of the 'Code Generation Strategy' designer
// property to an alternate value. This property is available in the Properties Window when the model is
// open in the designer.
Clarkin answered 25/10, 2012 at 18:0 Comment(0)
S
0

I think the issue is having an EntityModelCodeGenerator value in the Custom Tool field of the .edmx file properties. Just delete that value.

I think this is it, because I notice that in the project that I started off as an EF6 project, there is no value and it has no .designer.cs file, but in the two that I upgraded from EF4, they both have the value and the designer file.

(This equates to a <Generator> tag in the underlying .csproj file.)

Solemnity answered 22/12, 2015 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.