简体   繁体   English

如何在EF4中首先使用模型创建的表中添加表前缀?

[英]how can I add table prefixes to tables created using model first in EF4?

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. 我的客户端有一个db表命名约定,要求我为所有表添加一个特定于应用程序的前缀 - 比如“myapp_” - 我正在使用EF4和模型第一种方法。

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. 我可以指定一个自定义的db命名空间,但是他们不想为这个应用程序引入一个新的命名空间 - 他们的其他应用程序都不使用除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. 我沿着自定义t4模板的道路前进,但是在生成EF在运行时需要的3个元数据文件时,我无法指定工作流应该使用哪些t4模板。

Is there a preferred way to accomplish the table prefix requirement using EF? 是否有使用EF完成表前缀要求的首选方法?

Instead of several changes in CS Mapping and SSDL content you can also add the Table attribute to the EntitySet of the SSDL content. 您可以将Table属性添加到SSDL内容的EntitySet中,而不是对CS Mapping和SSDL内容进行多处更改。 This is much easier. 这更容易。 It becomes something like: 它变成了:

<EntitySet Name="Tests" EntityType="Test.Store.Tests" store:Type="Tables" Schema="dbo" *Table="Prefix_Tests"* />

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. 我不确定是否有用于生成数据库生成工作流将使用的edmx文件的模板,但它肯定使用一个用于DDL生成。 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. 在此文件夹中查找名为SSDLToSQL10.tt的文件您必须手动将此文件复制并粘贴到同一文件夹中,以便VS2010识别它,然后您可以修改和自定义它以基于您的表名前缀数据库命名约定。 Inside the T4 template, look for this code: 在T4模板中,查找以下代码:

foreach (EntitySet entitySet in Store.GetAllEntitySets())
           {
               string schemaName = Id(entitySet.GetSchemaName());
               string tableName = Id(entitySet.GetTableName());
               ...

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. T4中有3个地方有上面的代码,你需要应用前缀。 Once you are done save and close the file and go back to your EDM inside VS. 完成后保存并关闭文件并返回到VS内的EDM。 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. 现在,当您下载DDL生成模板属性时,您将看到复制和更改的新T4文件是可用选项。 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. 因此,可以修改模板并自定义DDL的构建方式。 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). 有一个难点:工作流程不使用修改后的T4来更改edmx文件以进行存储和映射定义(SSDL和MSL)。 So even though the database have been created correctly, the edmx still pointing to the original table names. 因此,即使数据库已正确创建,edmx仍指向原始表名。

Therefore, you'll need to open the edmx in XML editor and manually change CS mapping (MSL) and SSDL contents to have your prefixes. 因此,您需要在XML编辑器中打开edmx并手动更改CS映射(MSL)和SSDL内容以获得前缀。 For that you only need to change StoreEntitySet in mappings and the Name of EntitySets in SSDL content. 为此,您只需要在映射中更改StoreEntitySet ,在SSDL内容中更改EntitySet的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM