17

I have my database with table names starting with "tbl" prefix and column names like "ua_id" which are understandable in context of project but problematic if used in a model i.e names should be readable or meaningful(not like indicative names defined in database).

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

I want to map my tables by columns to my model as I can't change the column names. I just want the newer version of that syntax in the blog.

Thank You.

Manvendra
  • 173
  • 1
  • 1
  • 5

3 Answers3

27

If you are using Code First, you can simply decorate your model with Table and Column attribute and give it the 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

Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19
  • So what is the limitation of data annotation as compared to fluent api and which is better. – Manvendra Apr 09 '15 at 11:35
  • I would prefer the annotation mechanism and there is no limitation between the two approaches. It is just neater to put it inside as an annotation that searching the OnModelCreating code for the name. – Praveen Paulose Apr 09 '15 at 11:37
  • Ok. I'll try the data annotation first. Thank you for this blazing fast response.:-) – Manvendra Apr 09 '15 at 11:40
  • Both of the answers of Praveen and aSharma are worth marking as answer. I can mark only one but one should go with the example provided by Praveen+links provided by aSharma. Thanx to both of you. – Manvendra Apr 09 '15 at 12:58
7

You can keep following inside OnModelCreating

modelBuilder.Entity<MyModel>()
.Property(e => e.MyColumn).HasColumnName("DBColumn")
wonderbell
  • 1,126
  • 9
  • 19
  • Thank you @aSharma. So If i have two tables,I need to write the above syntax two times? And where should I define the name of database table. – Manvendra Apr 09 '15 at 11:31
  • Yes, it is required to write this statement for each column. That's why it is preferable to use Configuration classes for the models. You can refer to http://www.codeproject.com/Articles/165720/Using-the-Code-First-Model-Configuration-Classes and http://odetocode.com/blogs/scott/archive/2011/11/28/composing-entity-framework-fluent-configurations.aspx if you are interested in Fluent Configurations style. It is a personal choice to use one of the styles. You'd need to weigh your options against your needs as Annotations provide only a subset of Fluent functionality. – wonderbell Apr 09 '15 at 12:03
  • FYR - http://www.codeproject.com/Articles/476966/FluentplusAPIplusvsplusDataplusAnnotations-plusWor – wonderbell Apr 09 '15 at 12:03
  • Thank you for the links.I could not get this sentence:-"You'd need to weigh your options against your needs as Annotations provide only a subset of Fluent functionality" . Is fluent api better than data annotations? I meant what is that I can't do with data annotation as compared to fluent API. – Manvendra Apr 09 '15 at 12:11
  • I didn't say anything was better than the other, it is for you to decide according to your requirement. If you see the last link I posted, you'd understand what did I mean by 'subset'. You can search for the differences yourself as well. I have not used Annotations so can't talk much, but with Fluent it was easy to configure foreign key relations, cascade delete, RowVersion etc. – wonderbell Apr 09 '15 at 12:31
  • Ok.Currently I am going through your links. Thank you:-) – Manvendra Apr 09 '15 at 12:54
1

And if you're not using code-first, just select a table in the model diagram, hit F4 (properties) and change the name.

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
  • I am using code first. I am using it for first time and I found that generating a model is somewhat confusing to me. In code first I know what I am creating. Please tell me if I am wrong or any other idea is there. – Manvendra Apr 09 '15 at 11:27
  • In that case, go with @PraveenPaulose solution below. – simon at rcl Apr 09 '15 at 11:31