如何修改 edmx 的默认代码生成策略?
我想修改默认的代码生成策略,该怎么做?
我只想将类名从 <#=code.Escape(container)#>
修改为 Entities
并将默认连接字符串更改为 name=默认
。
(我不想为该项目创建模板文件,我想对其进行编辑,以便它可以在全球范围内工作)
我搜索了 .tt
文件,我只能找到 ItemTemplates。我不知道默认情况下生成代码的是什么,这是我要编辑的代码。
更新:我仍然不知道该怎么做。
I want to modify the default code generation strategy, how can I do that?
I simply want to modify the class name from <#=code.Escape(container)#>
to Entities
and change the default connection string to name=Default
.
(I don't want to create a template file for the project, I want to edit it so it will work globally)
I've searched for .tt
files, I could only find the ItemTemplates. I don't know what generates the code by default, this is the one I want to edit.
Update: I still don't know how to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果单击 EMDX 文件并在 Visual Studio 中检查文件属性,您可以看到生成代码的内容。查找
Custom Tool
属性,该属性将告诉您将 EDMX XML 转换为可编译代码的生成器的类名称。但关于模型定制,我仍然建议您使用 T4,它接受 EDMX 并生成与原始生成器相同的代码。好处是,如果你愿意的话,你可以操纵它,直到你死去。
如果您打算在项目中的多个 EMDX 上使用 T4,那么我建议您创建一个
.ttinclude
文件并在每个.tt
文件中引用它。这样,您将重用现有代码,并且当您更改它时,它将反映在所有生成的文件中。还有一个问题:全球是什么意思?全局适用于您的项目中的所有 EDMX 文件或您的计算机上的所有 EDMX 文件或您的项目团队中的所有 EDMX 文件还是什么?定义全局。
附加编辑
由于您已将全局定义为特定计算机上的所有项目,这就是我要做的。
首先:使用T4可以调整EDMX ->每个项目的代码转换都会发生变化,或者更好地说是每个解决方案(特定解决方案中的所有项目)。在同一台计算机上的其他项目/解决方案中,您应该包含相同的 T4 模板引用。因此,从您的意义上来说,它实际上并不是全局...
您可以做的最好的事情是使用此 T4 模板创建自定义 Visual Studio 项模板,这样添加此默认 T4 模板会容易得多您的解决方案/项目。这是您通过 T4 所能做到的最大程度的全球化。
也许您应该阅读此 MSDN 条目,其中讨论了您的自定义类型:
如何:自定义对象层代码生成(实体数据模型设计器)
You can see what generates the code if you click your EMDX file and check file properties in Visual Studio. Look for
Custom Tool
property that will tell you the class name of the generator that converts EDMX XML into compilable code.But regarding model customization, I would still suggest you use T4 that takes your EDMX and generates the same code as original generator. The good thing is that you can then manipulate it 'till you drop dead if you will.
And if you intend to use the T4 on several EMDXs in your project then I suggest you rather create a
.ttinclude
file and reference it in every.tt
file. This way you will reuse existing code and when you'd change it it will be reflected on all generated files.One more question: What do you mean by globally? Globally for all EDMX files in your project or for all EDMX files on your machine or all EDMX files on your project team or what? Define globally.
Additional edit
Since you've defined global as all projects on a particular machine this is what I'd do.
First of all: using T4 allows you to adjust EDMX -> code conversion changes per project or better said per solution (all projects in a particular solution). On other projects/solutions on the same machine, you should include the same T4 template reference. So it's not actually global in your sense...
The best thing you could do is to create a custom Visual Studio item template with this T4 template so it'd be much much easier adding this default T4 template to your solutions/projects. That's as global as you can make it by T4.
Maybe you should read this MSDN entry that talks about your kind of customization:
How to: Customize Object-Layer Code Generation (Entity Data Model Designer)
我不知道是否可以更改默认代码生成。
我想您可以创建一个从
ObjectContext
生成派生类的.tt
,而不是尝试修改默认代码生成。这样您就可以根据需要命名它并实现默认构造函数。类似于:
这种方法的缺点是您需要将这个
.tt
文件与您创建的每个 EDMX 一起部署。但是,使用 Visual Studio 的插件架构,您可以考虑创建一个模板,该模板默认创建 EDMX 和此
.tt
文件。作为添加普通“ADO.NET 实体数据模型”的替代方案,查看 EntityModelCodeGenerator(由默认 codegen 策略运行的自定义工具),它似乎已注册到
SingleFileGenerator
扩展机制,它是一个COM组件。更多信息 这里。I don't know whether it is even possible to alter the default code generation.
Instead of trying to modify the default code generation, I suppose you could create a
.tt
that generates a derived class from theObjectContext
. This way you can name it and implement the default constructor as you wish.Something like:
The downside to this approach is you will need to deploy this
.tt
file with every EDMX you create.However, with Visual Studio's addin architecture you could look into creating a template that creates an EDMX and this
.tt
file by default. As a replacement for adding a plain "ADO.NET Entity Data Model"Looking into the
EntityModelCodeGenerator
(the Custom Tool that is run by the default codegen strategy), it seems that it is registered with theSingleFileGenerator
extensibility mechanism, which is a COM component. Some more info here.