简体   繁体   中英

how can i change table name by fluent API of entity framework code first in c#?

我想通过 fluent API 更改我的 c# 类的表名,它必须映射到数据库(SQL Server)上,并且我首先在我的项目中使用实体框架代码作为 ORM。

For do that, you can create a folder as a configuration or mapping in your Data model layer, and then add a class like 'className+"Config".cs'. Then inherit that class form 'EntityTypeConfiguration<"MainClassName">' Then in that class you must create a constructor as same as a 'className+Config', and in this constructor you may use:

this.ToTable("Your_table_name_In_Sql", schemaName: "Your_Schema_name");

After all of them, add your 'className+Config' class to your "DbContext" class As i Mentioned below:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new 'className+Config'());

        base.OnModelCreating(modelBuilder);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM