Houman
Houman

Reputation: 66390

How to specify different table names than entities in EF4?

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

Answers (4)

LoxLox
LoxLox

Reputation: 995

In EF6 simply change the entityset name property in the entity property

HTH Lorenzo

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

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

Glory Raj
Glory Raj

Reputation: 17701

you can try like this ..

If you just need to change the name of the table you can:

  1. Open EDMX file with XML Editor.
  2. Locate SSDL section in it.
  3. Locate entity set element for example <EntitySet Name="Customers" EntityType="ExampleModel.Store.Customers" Schema="dbo" />.
  4. Add 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

Jobo
Jobo

Reputation: 1084

In your Context you can override OnModelCreating(DbModelBuilder modelBuilder) and do something like this:

modelBuilder.Entity<Contact>().ToTable("ab_Contact");

Upvotes: 0

Related Questions