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 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...
Solution:
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).
But single word class names are rather rare. Usually, we use noun phrases to provide more descriptive names for classes such as AnonymousUser, DirectMessage, or UserCreatedEvent. The less abstract a class is, the more details are present in its name. You should reserve verbs for method names as they represent actions executed by things.
Each column name should be unique. If two columns from different tables serving different purposes are in the same database then use some kind of prefixes that separate the two. Column names must not be abstract or cryptic. Use long descriptive names instead of short and unclear abbreviations.
Table names should be descriptive. If you are designing a table to store the data about customers in a grocery shop then the table can be named “Customer” or “Customers” based on your preference.
In that database, table names are defined as trip_tab , vehicletype_tab, driver_tab & etc. I want to create my model class names like Trip, VehicleType, Driver. and also controllers should be TripController, VehicleTypeController & etc. There is a most similar question in here.
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
Entity framework core offers the same option to map tablenames or columns
The mapping can be done by using attributes
[Table("blogs")]
public class Blog
{
[Column("blog_id")]
public int BlogId { get; set; }
public string Url { get; set; }
}
or by using the fluent api
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.ToTable("blogs");
modelBuilder.Entity<Blog>()
.Property(b => b.BlogId)
.HasColumnName("blog_id");
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With