How to match table names and non-model column names in the onmodelcreating method in Entity Framework -6?

I have my database with table names starting with the prefix "tbl" and column names such as "ua_id", which are understandable in the context of the project, but problematic if they are used in the model, i.e. Names must be readable or meaningful (not in the same way as defining names are specified in the database).

So I want to display them in my onmodelcreating method, but I have no idea about it. I studied it on the following blog:
http://weblogs.asp.net/scottgu/entity-framework-4-code-first-custom-database-schema-mapping but this is for EF 4.1 and the method does not work for EF 6. (mapsingletype method)

I want to map my tables column by column to my model as I cannot change the column names. I just need a newer version of this syntax on the blog.

Thanks.

+5
source share
3 answers

If you use Code First, you can simply decorate your model with the Table and Column attribute and give it database names.

[Table("tbl_Student")] public class Student { [Column("u_id")] public int ID { get; set; } } 

These attributes are available in the System.ComponentModel.DataAnnotations.Schema namespace

+8
source

You can continue to monitor OnModelCreating

 modelBuilder.Entity<MyModel>() .Property(e => e.MyColumn).HasColumnName("DBColumn") 
+3
source

And if you do not use the code first, just select the table in the model diagram, press F4 (properties) and change the name.

0
source

All Articles