本文介绍了在不同程序集中生成实体容器和模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些重构,并试图重用我的通用实体模型.我的应用程序有几个程序集,一个程序集是我的外部公共类型(API),一个程序集包含提供程序的实现(例如日志).

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

我想拆分实体和模型的生成,以便实体将位于API程序集中,而容器将位于实现程序集中.这可能吗?

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.

  • 组件A
    • Database.EDMX
    • Models.TT
    • Models.cs
    • Assembly A
      • Database.EDMX
      • Models.TT
      • Models.cs
      • Database.EDMX( 添加为链接到程序集A中的真实文件)
      • EntityContainer.TT
      • EntityContainer.cs
      • 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. 右键单击A(公共API程序集)中的EDMX,然后添加代码生成文件
      2. 将TT添加到项目中.之所以称为模型,是因为它仅包含模型.
      3. 编辑了TT并删除了实体容器的代码生成
      4. 在程序集B(内部实现)中添加了Database.EDMA作为链接
      5. 在程序集B中打开,右键单击并添加代码生成文件
      6. 向项目B添加一个TT.将其称为EntityContainer,因为它将仅包含该对象.
      7. 编辑TT以执行以下操作
        • 删除了实体创建步骤
        • 将Database.EDMX的路径更改为指向A中原始副本的相对路径
        • 为我的模型添加了using
      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.

      其他更改:

      在我的实体容器TT中,我不得不将EscapeEndTypeName的定义修改为以下内容:

      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));
      }
      

      我正在使用association.NamespaceName,因为它包含来自另一个程序集的正确名称空间.

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

      推荐答案

      我不知道答案,但我认为您的问题实质上等同于是否可以在一个项目中使T4模板发出代码?进入另一个项目?"如果可以做到,那么您可以做自己想做的.不过请注意,此在EF 4中更加容易.

      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.

      这篇关于在不同程序集中生成实体容器和模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:16