Is there a way to have class names be different from your table names?

前端 未结 2 2017
栀梦
栀梦 2020-12-19 10:18

We are using a database created several years ago, and would like to keep the table names the same.

All of our tables are named like: \"tbl_Orders\" but we would l

相关标签:
2条回答
  • 2020-12-19 10:24

    You can use following code in your DbContext to map all your entities to your tables:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       // TableNameConvention
         modelBuilder.Types()
                     .Configure(entity => 
                                entity.ToTable("tbl_" + entity.ClrType.Name));
    
         base.OnModelCreating(modelBuilder);
    }
    
    0 讨论(0)
  • 2020-12-19 10:43

    You can use the Table attribute or the fluent api to map between table names in your database and class names

    [Table("tbl_Blogs")] 
    public class Blog
    
    0 讨论(0)
提交回复
热议问题