开发者

Entity Container and Model generation in different assemblies

开发者 https://www.devze.com 2022-12-17 09:11 出处:网络
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

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 a开发者_如何转开发re 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.


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.

0

精彩评论

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

关注公众号