问题描述
我们使用的是在几年前一个数据库,想保持表名相同。
We are using a database created several years ago, and would like to keep the table names the same.
我们所有的表被命名为喜欢:tbl_Orders但我们希望的类名称为模特/控制器/等是订单/ OrdersController /等,我们正在使用映射实体框架类,以我们的表
All of our tables are named like: "tbl_Orders" but we would like the class names for the models / controllers / etc. to be Orders / OrdersController / etc. We are mapping the classes to our tables using Entity Framework.
很抱歉,如果这已被问过,我试图寻找,但来到了空手而归......
Sorry if this has been asked before, I tried searching but came up empty handed...
解决方案:
一些来回斯科特·张伯伦之后,我们得出的结论,这两个答案都是正确的。我说干就干,标志着马苏德的回答所接受,因为那是我去的路线。感谢对大家谁帮助(尤其是斯科特)。
After some back and forth 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 went. Thank's to everyone who helped (especially Scott).
推荐答案
您可以使用下面的code你的的DbContext
您的所有实体的表映射:
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);
}
这篇关于有没有办法让类名是从你的表名不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!