As a part of our application architecture, we like to define clear lines between our functional layers. A typical application solution, therefore, will contain:
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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With