C# – How to modify the default code generation strategy for edmx

centity-frameworkentity-framework-4templates

I want to modify the default code generation strategy, how can I do that?

I simply want to modify the class name from <#=code.Escape(container)#> to Entities and change the default connection string to name=Default.

(I don't want to create a template file for the project, I want to edit it so it will work globally)

I've searched for .tt files, I could only find the ItemTemplates. I don't know what generates the code by default, this is the one I want to edit.

Update: I still don't know how to do this.

Best Answer

You can see what generates the code if you click your EMDX file and check file properties in Visual Studio. Look for Custom Tool property that will tell you the class name of the generator that converts EDMX XML into compilable code.

But regarding model customization, I would still suggest you use T4 that takes your EDMX and generates the same code as original generator. The good thing is that you can then manipulate it 'till you drop dead if you will.

And if you intend to use the T4 on several EMDXs in your project then I suggest you rather create a .ttinclude file and reference it in every .tt file. This way you will reuse existing code and when you'd change it it will be reflected on all generated files.

One more question: What do you mean by globally? Globally for all EDMX files in your project or for all EDMX files on your machine or all EDMX files on your project team or what? Define globally.

Additional edit

Since you've defined global as all projects on a particular machine this is what I'd do.

First of all: using T4 allows you to adjust EDMX -> code conversion changes per project or better said per solution (all projects in a particular solution). On other projects/solutions on the same machine, you should include the same T4 template reference. So it's not actually global in your sense...

The best thing you could do is to create a custom Visual Studio item template with this T4 template so it'd be much much easier adding this default T4 template to your solutions/projects. That's as global as you can make it by T4.

Maybe you should read this MSDN entry that talks about your kind of customization:
How to: Customize Object-Layer Code Generation (Entity Data Model Designer)