Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have class names be different from your 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 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).

like image 506
Primalpat Avatar asked Oct 06 '14 15:10

Primalpat


People also ask

Can a class have a single word name?

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.

How do you name a column in a database?

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.

What should be the name of a table?

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.

What are the table names of the model class?

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.


2 Answers

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

3rd party edit

Entity framework core offers the same option to map tablenames or columns

  • map tables names
  • map column names

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");
}
like image 62
ErikEJ Avatar answered Nov 09 '22 17:11

ErikEJ


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);
}
like image 26
Masoud Avatar answered Nov 09 '22 16:11

Masoud