EF Code Generation is cool and all. But for some, we want separation of concern, particularly in this case, the types that generator generates.
This is how my EDMX looks like after adding ADO.Net Entity Data Model.
What we are going to do is to move Model.tt to another project.
Create the project. And move the file to the new project.
In Visual Studio 2012, I can’t drag and drop the file, so I have to browse to project’s folder and copy it manually to another project’s folder. Note that only Model.tt needs to be moved.
After copying Model.tt file, you will see this in Visual Studio’s Solution Explorer after clicking Refresh and Show All Files buttons located on top of the Solution Explorer.
Right click on the Model.tt in the new project to include the file in the project. If you receive errors, that’s fine, we will fix it.
Delete Model.tt file in the old project.
Open Model.tt file in Visual Studio and change the input file for the template file to point to EDMX file located on the project (shown below on line 5). Save the file.
At this point, you will see the template file has generated all entity types in the new project you just created.
Since the entity types have moved, the namespace has also changed. Thus, we will need to change namespace of the context template so that the compiler will find the entity types.
Open up property browser window of Model.Context.tt template file in the old project.
Change Custom Tool Namespace property to namespace that the Model.tt is located. You can open one of the files generated by Model.tt to see the namespace.
In the old project, add reference to the new project.
Build the solution to make sure everything works.