Reputation: 66390
I have a number of entities in my EF4-Model like Contacts
, Calls
, ContactTypes
etc.
When I generate the DB of the model, I get table names with the same naming.
How would I set the table names with a prefix ab_Contacts
, ab_Calls
etc but yet leave the entities as Contacts
and Calls
in the generated code?
I wasn't able to find a way to specify this in the .edmx file.
Upvotes: 3
Views: 2947
Reputation: 995
In EF6 simply change the entityset name property in the entity property
HTH Lorenzo
Upvotes: 0
Reputation: 364409
If you use model first you should not need this. Model first is for scenarios where you have no requirements on your database.
Changing this behavior requires change to generation process of SSDL part of your EDMX (which is created once you ask designer to generate SQL for you). By default this process is handled by workflow (%VSINSTALLDIR%\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\TablePerTypeStrategy.xaml
) or it can be handled by T4 templates if you install Database Generation Power Pack. You will have to modify either workflow (implement some custom generation component) or T4 template to use your custom naming convention.
Edit:
OK. I decided to try it myself to make the answer complete even I believe that pointing to the correct way should be enough.
When you install the Database Generation Power Pack you get a new set of templates and workflows installed on the above mentioned path. One of the workflows is called Generate T-SQL Via T4 (TPT).xaml
. The difference between this workflow and original TablePerTypeStrategy.xaml
is that this one is using external T4 templates to generate SSDL and MSL. What you need to do is create copy of this workflow and place it to the same directory (you must get it a different name). You must also create a copy of refereced T4 template for SSDL creation called CSDLToSSDL_TPT.tt
and also place it to the same directory (with different name). Once you have copy of .tt file open the new .xaml file and change path to the SSDL template to the new one (either in designer in VS or in any text editor). As last step you must open your copy of .tt file and update EntitySet
element creation:
foreach (EntityType entityType in edm.GetAllEntityTypes()) { #>
<EntitySet Name="<#=WriteEntityTypeName(entityType, edm)#>" EntityType="<#=ssdlModelNamespace#>.<#=WriteEntityTypeName(entityType, edm)#>" Table="prefix_<#=WriteEntityTypeName(entityType, edm)#>" store:Type="Tables" Schema="<#=databaseSchemaName#>" />
<# }
I have added Table
attribute with name specifying prefix_
(you can replace it with your own). Now you must select your new workflow in Generate database from model wizard and create a new SQL script.
Upvotes: 2
Reputation: 17701
you can try like this ..
If you just need to change the name of the table you can:
<EntitySet Name="Customers" EntityType="ExampleModel.Store.Customers" Schema="dbo" />
. Table="MyTableName"
attribute. <EntitySet Name="Customers" EntityType="ExampleModel.Store.Customers" Schema="dbo" Table="MyTableName" />
Here is a complete CSDL, SSDL, MSL specification.
Hope that helps.
taken from this link
Upvotes: 1
Reputation: 1084
In your Context you can override OnModelCreating(DbModelBuilder modelBuilder) and do something like this:
modelBuilder.Entity<Contact>().ToTable("ab_Contact");
Upvotes: 0