简体   繁体   English

c# - 如何通过实体框架代码的流畅API首先在c#中更改表名?

[英]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'.为此,您可以在数据模型层中创建一个文件夹作为配置或映射,然后添加一个类,如“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:然后继承该类形式 'EntityTypeConfiguration<"MainClassName">' 然后在该类中,您必须创建一个与 'className+Config' 相同的构造函数,并且在此构造函数中您可以使用:

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:毕竟,将您的“className+Config”类添加到您的“DbContext”类中,如下所述:

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

        base.OnModelCreating(modelBuilder);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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