it:ad:ef:howto:move_the_generation_of_entities_to_a_domain.entities_assembly

IT:AD:EF:HowTo:Move the Generation of Entities to a Domain.Entities Assembly

Summary

Having the DbContext in the same assembly as the Entities has a terrible cost:

  • everywhere you'll be working with the entities, you'll have to include refs to EF.

You really have to separate them into two projects.

Note:

T

here is small disadvantage of using template in another project - it will not update automatically when you modify model.

>You must always trigger entity recreation manually by using Run custom tool from context menu on template file.

Once you've created a Database model:

it's time to move the Entities *.tt from the first assembly, to the MyApp.Domain.Entities assembly.

  • Cut/Paste the Entities.tt from the current assembly into the MyApp.Domain.Entities assembly.
Note that in VS2010 you could just drag/drop it.

>In 2012, the *.tt is now nested under the model, and the proccess is a bit more convoluted (as described [here](http://stackoverflow.com/questions/12200258/visual-studio-2012-cant-move-ef-tt-files)).

The VS2012 process is as follows:
* Save the project. * Right click the project, and Open Directory (so you can see the .csproj): Use Notepad++ to edit the *.csproj, find the *.tt part, and remove the DependentUpon part:

Example:

    
  <None Include="Model.tt">
    <Generator>TextTemplatingFileGenerator</Generator>
    <!-- <DependentUpon>Model.edmx</DependentUpon> -->
    <LastGenOutput>Model.cs</LastGenOutput>
  </None>

Saving the changes will cause Visual Studio to prompt you to reload the project.

The *.tt file will now be unhooked from the *.edmx, and easy to cut/paste/move it (and the entities automatically generated under it) to the MyApp.Domain.Entities assembly.

Delete the entities in order to better test the follwing steps.

If you right-click and try to 'Run Custom Tool' you'll get an exception, and no entities will be created. That's because the .tt can't find the *.edmx it relies on. Edit the *.tt file, and change the following to point

From:

const string inputFile = @"..\XAct.Spikes.DbFirst.Infrastructure.Data\MyModel.edmx";

To:

const string inputFile = @"..\XAct.Spikes.DbFirst.Infrastructure.Data\MyModel.edmx";

* Now retry the 'Run Custom Tool' and the entities will get built in the Entities assembly.

You're still not going to build property.

The issue is the default DbContext cannot generate DbSet's for the Entities unless it can find them. But the namespaces don't match.

The solution is two parts:

  • Ensure the namespace of the generated assemblies is correct.
    • Note that in one case I did update it, but was not getting satisfaction. Try Rebuild All after ensuring everything is saved…

Go back to the *.tt that generates the DbContext.

Add

using <#=modelNamespace#>;

to line 46 so that Entities can be found in the Corp.App.Domain namespace

Errata

An alternate way is as follows, but I don't recommend it, as it makes a circular ref from Domain Entities looking at Infrastrcture.Data, rather than the other way around:

  • Move the Entities to the Entities assembly
    • Turn off code generation for the template (set the MyModel.tt entity template file's Custom Tool property to an empty string).
    • Expand the tree for the entity template file and delete all of its automatically generated items.
    • Add an existing item and browse to your entity template file, but add it as a link (do not add it directly).
      • Adding it as a link will allow the model and the template to stay in sync, but the code generation will occur in the new assembly.