Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 4.1 DbContext Generattor - Put Entities in different project?

As a part of our application architecture, we like to define clear lines between our functional layers. A typical application solution, therefore, will contain:

  • Entity
  • Model
  • Task
  • Presenter
  • FrontEnd

These end up being completely distinct assemblies.

The Entity/Model delineation is done to keep database access functionality in a separate layer from our POCOs, so that only Task ever need know about Model, while everyone up to Presenter knows about Entity

This works well when using Code-First or Fluent-API - but due to the lack of support for SPROCs in those paradigms, it turns out that under EF 4.1 I must use EDMX models.

So - I'm generating POCOs using a DbContext Generator, but the resulting classes end up under .Model, and while I can force their namespace into .Entity instead, they still live in the .Model assembly, which means now .Presenter must reference .Model to get to classes that should be in .Entity.

Is there a way to force or trick EF to dump its generated output into a different Project?

like image 364
The Evil Greebo Avatar asked Jun 14 '11 19:06

The Evil Greebo


People also ask

How do I add Entity Framework to an existing project?

Install Entity Framework to your Project. Right click on your project name and select Manage NuGet Packages. Go to Browse and Select Entity Framework then click Install button to install Entity Framework on your project.

How do you change the state of entity using DbContext?

This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.


1 Answers

Sure. DbContext Generator are just two T4 templates. You can move the template generating entities to other project. You just need to modify template to point to correct EDMX file. This is default:

string inputFile = @"Model.edmx";

You must change it to relative address to your EDMX file. It will be something like:

string inputFile = @"../Model/Model.edmx"

The template will automatically use default namespace of current project for generated entities but you will have to modify the second template for context to use the new namespace so that entity types are correctly resolved from referenced assembly.

There 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.

like image 91
Ladislav Mrnka Avatar answered Nov 15 '22 04:11

Ladislav Mrnka