如何将表前缀添加到在 EF4 中使用模型优先创建的表?
我的客户有一个数据库表命名约定,要求我为所有表添加特定于应用程序的前缀 - 例如“myapp_” - 我使用模型优先方法的 EF4。
我可以指定自定义数据库命名空间,但他们不想为此应用程序引入新的命名空间 - 他们的其他应用程序都不会使用 dbo 以外的命名空间。
我走上了自定义 t4 模板的道路,但无法指定工作流程在生成 EF 在运行时所需的 3 个元数据文件时应使用哪些 t4 模板。
是否有使用 EF 完成表前缀要求的首选方法?
My client has a db table naming convention that requires me to prefix all tables with an application specific prefix - like "myapp_" - I am using EF4 with the model first approach.
I can specify a custom db namespace but they do not want to introduce a new namespace for this app - none of their other apps use namespaces other than dbo.
I went down the path of customizing the t4 templates but I could not specify which t4 templates the workflow should use when generating the 3 metadata files that EF needs at runtime.
Is there a preferred way to accomplish the table prefix requirement using EF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以将 Table 属性添加到 SSDL 内容的 EntitySet,而不是对 CS 映射和 SSDL 内容进行多项更改。这要容易得多。它变成这样:
Instead of several changes in C-S Mapping and SSDL content you can also add the Table attribute to the EntitySet of the SSDL content. This is much easier. It becomes something like:
我不确定数据库生成工作流程是否会使用一个用于生成 edmx 文件的模板,但它肯定会使用一个模板来生成 DDL。位于此路径:
\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen
在此文件夹中查找名为
SSDLToSQL10.tt
的文件,您必须手动将此文件复制并粘贴到同一文件夹中,以便 VS2010 识别它,然后您可以修改和自定义它以根据您的数据库命名约定为表名称添加前缀。在 T4 模板内,查找以下代码:
您可以更改表名称以具有自定义前缀。 T4内有3个地方有上述代码,需要应用前缀。完成后保存并关闭文件并返回到 VS 内的 EDM。现在,您将看到,当您下拉 DDL Generation Template 属性时,您复制和更改的新 T4 文件是一个可用选项。选择它,现在当您从模型生成数据库时,您会发现您的表名称具有前缀
因此,可以修改模板并自定义 DDL 的构建方式。有一个棘手的问题:工作流程不使用修改后的 T4 来更改存储和映射定义(SSDL 和 MSL)的 edmx 文件。因此,即使数据库已正确创建,edmx 仍然指向原始表名。
因此,您需要在 XML 编辑器中打开 edmx,并手动更改 CS 映射 (MSL) 和 SSDL 内容以包含您的前缀。为此,您只需更改映射中的 StoreEntitySet 和 SSDL 内容中的 EntitySet 名称。
I'm not sure if there is a template for generating edmx files that the Database Generation Workflow would be using, but it definitely use one for DDL generation. which is located at this path:
<Program Files>\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen
Inside this folder look for the file named
SSDLToSQL10.tt
You’ll have to manually copy and paste this file in the very same folder in order for VS2010 to recognize it and then you can modify and customize it to prefix the table names based on your DB naming convention.Inside the T4 template, look for this code:
You can change the table name to have your custom prefix. There are 3 places inside the T4 that has the above code and you need to apply the prefix. Once you are done save and close the file and go back to your EDM inside VS. You'll see that now when you drop down the DDL Generation Template property, the new T4 file that you copied and changed is an available option. Select that and now when you generate database from model, you'll find your table names have the prefix
Therefore, it’s possible to modify the template and customize how the DDL is built. There is a catch tough: The Workflow does not use the modified T4 to change the edmx file for storage and mappings definitions (SSDL & MSL). So even though the database have been created correctly, the edmx still pointing to the original table names.
Therefore, you'll need to open the edmx in XML editor and manually change C-S mapping (MSL) and SSDL contents to have your prefixes. For that you only need to change StoreEntitySet in mappings and the Name of EntitySets in SSDL content.