修改实体框架类型T4模板 [英] Modifying Entity Framework Types T4 Template

查看:42
本文介绍了修改实体框架类型T4模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改Entity Framework 6(模型优先)用于生成类型的T4模板,以便在所有实体类定义上注入 GeneratedCodeAttribute .我希望无论何时在任何项目中以任何解决方案添加新的ADO.NET实体数据模型,该功能都将持续存在-因此,我需要对除随实体图生成的T4模板以外的其他内容进行更改.

EF6专家可以建议我如何去做吗?

这是我尝试过的方法(未成功):我找到了一个名为CSharpDbContext.Types.tt的T4模板,其内容似乎与生成的T4模板相同我的.edmx文件.我假设此文件是EF 6.x DbContext Generator,该文件显示在Visual Studio的添加新项"列表中.我的计算机上CSharpDbContext.Types.tt的文件位置是:

C:\ Program Files(x86)\ Microsoft Visual Studio 12.0 \ Common7 \ IDE \ ItemTemplates \ CSharp \ Data \ 1033 \ DbCtxCSEF6

但是,当我修改该文件时,我得到的类定义与修改前完全相同.如果将其移动到完全不同的位置,则将再次获得相同的结果.据我所知,此文件与我的edmx专用T4模板之间没有任何联系.

感谢有人能对此有所启发.

解决方案

第1步:不要这样做.请勿修改通过EF下载的模板,因为您将无法轻松,一致地将其复制到干净"的计算机上.

第2步:首先添加EF DB后,您可以在项目中编辑模板的副本.该模板将存储在代码存储库中,您无需设置魔术"即可获得可重复的结果.缺点是,每次创建新的EF上下文时,您都需要修改模板.

要修改模板,请打开MyEFItem.tt,研究其工作原理并根据需要对其进行编辑.在您的情况下,我将在该.tt中搜索部分类",然后在其之前的行中插入[GeneratedCode].保存后,您将立即看到生成的.cs中的更改,也可以右键单击.tt和运行自定义工具"

I would like to modify the T4 template that Entity Framework 6 (model-first) uses to generate types in order to inject a GeneratedCodeAttribute on all entity class definitions. I want this feature persisted any time I add a new ADO.NET Entity Data Model, in any project, in any solution - so I need to make the change in something other than the T4 template which gets generated with my entity diagram.

Could an EF6 expert advise how I would go about doing this?

This is what I've tried (without success): I located a T4 template called CSharpDbContext.Types.tt, the contents of which appear to be identical to the T4 template that gets generated with my .edmx file. I assume this file is the EF 6.x DbContext Generator that shows up in the Visual Studio Add New Item list. The file location of CSharpDbContext.Types.tt on my computer is:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Data\1033\DbCtxCSEF6

When I modify this file, however, I get exactly the same class definition as I do before the modification. If I move it to an entirely different location, I again get the same result. From what I can tell, there is no connection between this file and my edmx-specific T4 template.

Appreciate if someone can shed some light on this.

解决方案

Step 1: Don't do that. Don't modify the template that's downloaded with EF because you won't be able to easily and consistently replicate that on a "clean" machine.

Step 2: Once you add EF DB-first you can edit the copy of the template inside your project. This template will be stored in code repository and you'll be able to get repeatable results without "magical" setup. The drawback is that you'll need to modify the template every time you create a new EF context.

To modify the template open MyEFItem.tt, study how it works and edit it as needed. In your case I'd search for "partial class" in that .tt and insert the [GeneratedCode] on the line before that. As soon as you save you'll see the changes in the generated .cs, or you can right-click on the .tt and "Run Custom Tool"

这篇关于修改实体框架类型T4模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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