不同程序集中的实体容器和模型生成
我正在做一些重构,并尝试重用我生成的实体模型。我的应用程序有一些程序集,其中一个是我的外向公共类型 (API),另一个包含提供程序的实现(例如日志)。
我想拆分实体和模型的生成,以便实体位于 API 程序集中,容器位于实现程序集中。这可能吗?
有可能。我就是这样做的。
- 装配A
- 数据库.EDMX
- 模型.TT
- 模型.cs
- 集 B
- Database.EDMX(添加为链接到程序集A中的真实文件)
- EntityContainer.TT
- EntityContainer.cs
这就是一切的布局方式。大致步骤如下:
- 右键单击 A(公共 API 程序集)中的 EDMX,然后添加代码生成文件
- 将 TT 添加到项目中。将其称为模型,因为它仅包含模型。
- 编辑了 TT 并删除了实体容器的代码生成
- 在程序集 B(内部实现)中添加了 Database.EDMA 作为链接
- 在程序集 B 中打开,右键单击并添加代码生成文件
- 将 TT 添加到项目 B。将其称为 EntityContainer,因为它将包含仅此而已。
- 编辑 TT 执行以下操作
- 删除了实体创建步骤
- 将 Database.EDMX 的路径更改为指向 A 中原始副本的相对路径
- 为我的模型添加了
using
希望这一切都能正确编译和工作(我'我距离编译和测试所有内容还很远)。到目前为止看起来不错。
其他更改:
在我的实体容器 TT 中,我必须将 EscapeEndTypeName 的定义修改为以下内容:
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 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:
- Right click on the EDMX in A (public API assembly) and Add Code Generation File
- Adds a TT to the project. Called it Models, as it will contain the models only.
- Edited the TT and removed code generation for entity containers
- In assembly B (internal implementations) added Database.EDMA as a link
- Opened in assembly B, right click and Add Code Generation File
- Adds a TT to project B. Called it EntityContainer as it will contain that only.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道答案,但我认为你的问题本质上相当于“是否有可能导致一个项目中的 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.