Is there a way to make class names differ from table names?

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: "tbl_Orders", but we would like the class names for the models / controllers / etc. were Orders / OrdersController / etc. We map classes to our tables using the Entity Framework.

Sorry if this was asked before, I tried searching but came up empty-handed ...

Decision:

After a few years ago and with Scott Chamberlain, we came to the conclusion that both answers are correct. I went ahead and marked Masoud's answer as accepted because that is the route I took. Thanks to everyone who helped (especially Scott).

+3


source to share


2 answers


You can use the following code DbContext

to map all of your objects to tables:



protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   // TableNameConvention
     modelBuilder.Types()
                 .Configure(entity => 
                            entity.ToTable("tbl_" + entity.ClrType.Name));

     base.OnModelCreating(modelBuilder);
}

      

+4


source


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



[Table("tbl_Blogs")] 
public class Blog

      

+4


source







All Articles