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.
If you just need to change the name of the table you can:
- Open EDMX file with XML Editor.
- Locate SSDL section in it.
- Locate entity set element for example
<EntitySet Name="Customers" EntityType="ExampleModel.Store.Customers" Schema="dbo" />
. - 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.
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"
}
}
I believe what you are asking is reconfigure the mapping of an entity to a table. You can right click an entity and select table mapping. This would show you the entity the table is mapped. You can change the table in there. However when you open the dropdown, you will only see the table that you have imported using the Update Model wizard. If the table is not imported, it will not be listed. You can then map the properties appropriately to the column names of the table.