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

前端 未结 2 2023
栀梦
栀梦 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);
    }
    

提交回复
热议问题