如何调整实体框架POCO T4模板在单独的项目中创建类? [英] How do I adapt the Entity Framework POCO T4 template to create classes in a separate project?

查看:96
本文介绍了如何调整实体框架POCO T4模板在单独的项目中创建类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢将代码保持在第一位 - 或者T4生成 - POCOs在与DbContext分离的项目中。这有助于我确保我的实体类没有耦合到任何一个数据访问服务。

I like to to keep my code first - or T4 generated - POCOs in a project separated from the DbContext. This helps me ensure my entity classes aren't coupled to any one data access service.

当我创建一个新的DB第一个EDMX模型时,该向导将生成一个T4模板,以生成与DbContext相同的项目中的所有POCO。如何修改此模板以将类添加到单独的项目?

When I create a new DB first EDMX model, the wizard generates a T4 template to generate all the POCOs in the same project as the DbContext. How can I modify this template to add classes to a separate project?

仔细检查后,将DbContext移动到新项目可能会更容易,但是T4为此没有调用 fileManager.StartNewFile 所以我不知道从哪里开始告诉它在其他地方创建一个文件。

On closer inspection, it would probably be much easier to move the DbContext to a new project, but the T4 for this has no call to fileManager.StartNewFile so i don't know where to begin telling it to create a file elsewhere.

推荐答案

您可以从DAL项目中排除.tt文件,然后将作为链接添加到另一个项目中。

You can exclude the .tt file from your DAL project and then add it as a link in another project.

这意味着您不必更改模板,因为它可以看到您的模型。

This means you won't have to alter the template as it can see your model.

运行模板时生成的文件将包含在您的数据对象程序集中,尽管物理文件将位于DAL项目中。

The files produced when you run the template will be included in your data objects assembly, although the physical files will be in your DAL project.

唯一的缺点是您必须手动运行自定义工具你更新你的模型。

The only downside is that you will have to manually run the custom tool when you update your model.

这篇关于如何调整实体框架POCO T4模板在单独的项目中创建类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆