Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change db table name in EF4 (entity framework 4)

Does anyone know how to change the mapped db table for an entity in EF4 (entity framework 4)?

Later edit: I think i've found the place where the table names are defined, in the model browser. But their names are readonly, so it's not possible to edit them using the designer. Also, there's no reference (from what i've searched) to the table name in the xml schema.

like image 248
scripni Avatar asked Jul 18 '10 19:07

scripni


2 Answers

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.

like image 54
Yury Tarabanko Avatar answered Nov 03 '22 16:11

Yury Tarabanko


An alternative solution would be to override a method in your DbContext class.

public class MyDbContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>().ToTable("DB_PRODUCTS_TBL");
        // otherwise EF assumes the table is called "Products"
    }
}
like image 20
Tomislav Markovski Avatar answered Nov 03 '22 16:11

Tomislav Markovski