Entity Framework with Poco object using Data Annotations for database tables columns mapping
Posted on: March 18, 2012
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
1using System.ComponentModel.DataAnnotations;
This is pretty easy but as the disadvantage to corrupt your Poco object with database indicator (columns name).
1public class MyObject { public int ID { get; set; }23 [Column("Code_SuperWeirdTableColumnName")]4 public string Code{ get; set; }56 [Column("Name_EN_USA")]7 public string Name { get; set; }8}
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
.