views:

344

answers:

3

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.

+1  A: 

You can execute a stored proc that changes the table name, passing the tablename as a variable. Then import the stored proc into EF4.

CREATE PROCEDURE ChangeTableName

@TableName varchar(200)

AS BEGIN SET NOCOUNT ON;

    EXEC sp_rename "User", @TableName

END GO

fARcRY
But i can't see how importing the stored procedure will update the schema. I think i didn't make clear what i'm trying to achieve, i'm trying to change the table to which an ef schema is mapped to, for example now it's retrieving data from table x in the database, i want it to retrieve data from table y now, both tables exist in the database, both have the exact same structure. The reason why i can't simply import data from one table to the other is because one table is used by some applications that can't be modified, and all applications need to access the same data. Thanks for the reply.
scripni
I see the problem. Need to dynamically update the EF Schema, after renaming the table.
fARcRY
+2  A: 

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.

Yury Tarabanko
A: 

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.

zeeshanhirani