Entity Container and Model generation in different assemblies

时光毁灭记忆、已成空白 提交于 2019-12-22 08:19:47

问题


I'm doing some refactoring and am trying to reuse my genertated entity models. My application has a few assemblies, one being my outward facing public types (API) and one containing implementations of providers (such as the log).

I'd like to split the generation of the entities and models so that the entities will be in the API assembly and the container will be in the implementation assembly. Is this possible?


Is possible. This is how I did it.

  • Assembly A
    • Database.EDMX
    • Models.TT
    • Models.cs
  • Assembly B
    • Database.EDMX (Added as a Link to the real file in Assembly A)
    • EntityContainer.TT
    • EntityContainer.cs

That's how everything is laid out. These are the rough steps:

  1. Right click on the EDMX in A (public API assembly) and Add Code Generation File
  2. Adds a TT to the project. Called it Models, as it will contain the models only.
  3. Edited the TT and removed code generation for entity containers
  4. In assembly B (internal implementations) added Database.EDMA as a link
  5. Opened in assembly B, right click and Add Code Generation File
  6. Adds a TT to project B. Called it EntityContainer as it will contain that only.
  7. Edited TT to do the following
    • Removed entity creation steps
    • Changed the path to Database.EDMX to a relative path pointing at the original copy in A
    • Added a using for my models

Hopefully this will all compile and work correctly (I'm still far from getting everything compiled and tested). Looks good so far.


Additional change:

In my entity container TT, I had to modify the definition of the EscapeEndTypeName to the following:

string EscapeEndTypeName(AssociationType association, int index, 
    CodeGenerationTools code)
{
    EntityType entity = association.AssociationEndMembers[index]
      .GetEntityType();
    return code.CreateFullName(
      code.EscapeNamespace(association.NamespaceName), code.Escape(entity));
}

I'm using association.NamespaceName as it contains the correct namespace from the other assembly.


回答1:


I don't know the answer, but I think that your question is essentially equivalent to "Is it possible to cause a T4 template in one project to emit code into a different project?" If you can do that, then you can do what you want. Note, though, that this is substantially easier in EF 4.

So I think you might get useful feedback if you asked that question directly.



来源:https://stackoverflow.com/questions/2104641/entity-container-and-model-generation-in-different-assemblies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!