Change db table name in EF4 (entity framework 4)

后端 未结 6 2098
离开以前
离开以前 2020-12-01 18:53

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 def

相关标签:
6条回答
  • 2020-12-01 19:05

    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.

    0 讨论(0)
  • 2020-12-01 19:06

    In the Model Browser or design surface right-click the entity and select properties. In the properties window edit the field "Entity Set Name".

    This worked for me, however I was designing the schema first and then generating the database.

    0 讨论(0)
  • 2020-12-01 19:08

    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"
        }
    }
    
    0 讨论(0)
  • 2020-12-01 19:10

    Because I created my database first, I did the following:

    1. Backed up the *.edmx file.
    2. Changed my database table name.
    3. Did as you suggested by renaming the Entity Set Name in the properties.
    4. I then Updated the model based on my database by right-clicking the entity.
    5. I noticed that the *.edmx file was missing half the lines, so I overlayed the *.edmx file with my backup, opened it in notepad, and did a replace all of my old table name with my new table name.
    6. Rebuilt the MVC Application, tested, and it worked.

    Note: Whether all these steps above are needed I don't know, it's just what I tried and it worked for me.

    0 讨论(0)
  • 2020-12-01 19:11

    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.

    0 讨论(0)
  • 2020-12-01 19:13

    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
    
    0 讨论(0)
提交回复
热议问题