开发者

EF 4.1 DbContext Generattor - Put Entities in different project?

开发者 https://www.devze.com 2023-03-13 04:25 出处:网络
As a part of our application architecture, we like to define clear lines between our functional layers.A typical 开发者_开发技巧application solution, therefore, will contain:

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号