SO you have decided to use an ADO.Net Entity Data model in your solution. But Visual Studio automatically creates all the Data Model classes under your Entity Data Model file structure. You want to separate your Data Model classes into another project, so that you can add references to the Data Model classes without exposing your Data Model Contexts and connections. In this article, I will show how you can achieve this in a few basic steps..

So you want a clean application architecture for your projects and classes. You want to easily map your database tables and sql/linq queries to your data model classes, however you don’t want to expose your Entity Data Model project (especially the DB Context etc) to your UI Layer, or other layers that shouldn’t have this access.

You want a separation of concerns. You only want your Repository project to have this reference to your Entity Data Model project. Yet, you want to re-use the Data Model classes, but don’t want to duplicate the classes in your Entity project (the project that contains your POCO, Presentation models and other classes you want to refernce in your UI layer). In comes the Entity Framework DBContext Generator.

The EF DBContext Generator generates files with the extension *.tt

The Text Template (TT) file is the parent file whereby all your EF classes stem from. In order to separate your EF DB Model from your Entitiy classes, it’s the simple operation of creating the new *.tt file in your Entity Project, and pointing this file to your Data Model.

Below I will show you the before and after pics of this splitting process:

1. Creating the Data Model

Let us create a Data Model (I called mine BudgetingModel.edmx) inside the DataModel Project – This is the project that will provide data access via our repositories and db contexts..


As you see below, we have our edmx file inside the DataModel project.


Opening this file in Visual Studio and you will see your model diagram:


2. Finding our class files

If you expand the edmx file, you will notice a handful of files that Visual Studio generated for us. Amongst this list is the file (highlight below). Expand this file, and there you have it!  All the generated classes, tightly packed under the data model structure.


3. The Split

So, this is where it gets interesting.. lets do the split:

Create another project called “Entity”. This project will house all your shared entities. The idea is that the Entity project will be referenced in our other projects (whether it be our UI or Business layer).


Add a new Item to your Entity proejct.  Select the EF DBContext Generator file type, call it “” (as below):


Now the key part in linking the *.tt file to your DB Model class is in the first couple of lines.

Double click to open the *.tt file, and you should see the following lines of code.

Edit the line that begins with “const string inputFile =”, and replace the value with the relative path to your edmx file. The edited file with the correct path is shown below:


Once that is done, save your file.. and compile…  voila!
You should now have all your db entitiy classes appear.


4. Cleaning Up

a. Navigate back into your DataModel project, and remove the old *.tt file as this is no longer required.

b. Click on the * file, and edit the Custom Tool Namespace.


Change this to the Namespace of your Entity Project (in this example it is “Budgeting.Entity”). This simple puts the context file in the same namespace as your entities. Otherwise, you may have compilation errors.


You have now successfully separated your auto generated entity classes from your data model 🙂

Thanks for reading.!