Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Entity Framework with Poco object using Data Annotations for database tables columns mapping

Posted on: 2012-03-18

It's possible when using Poco object with Entity Framework to use DataAnnotation or to use the Fluent API.

The DataAnnotation require to add a reference to System.Component.Model.DataAnnotations

using System.ComponentModel.DataAnnotations; 

This is pretty easy but as the disadvantage to corrupt your Poco object with database indicator (columns name).

public class MyObject { public int ID { get; set; } 

  [Column("Code_SuperWeirdTableColumnName")] 
  public string Code{ get; set; } 

  [Column("Name_EN_USA")] 
  public string Name { get; set; } 
} 

As you can see in the example above, the property Code is linked to Code_SuperWeirdTableColumnName which let you have a proper name inside your model object and still use another name into the table. It's the same for the Name of the MyObject class. It will be linked to Name_EN_USA.