I've just started to implement EF CTP 5 in to a new project. In this case all my database fields are named differently to my POCO properties due to an obscure database naming convention. Am I right in thinking the best way to map this is to override OnModelCreating and have code like this
modelBuilder.Entity<Sale>().Property(s => s.ID).HasColumnName("sale_id");
modelBuilder.Entity<Sale>().Property(s => s.ProductName).HasColumnName("product_name");
modelBuilder.Entity<Sale>().Property(s => s.ProductPrice).HasColumnName("product_price");
modelBuilder.Entity<Sale>().Property(s => s.SaleDate).HasColumnName("sale_date");
This is going to end up very large very quick, is it really th开发者_StackOverflowe best way to do this?
I think you should consider using the new CTP5 Column attribute. Using data annotations seems to be a better choice than fluent API in this case. Your Sale class will look like the code below:
public class Sale
{
[Column(Name="sale_id")]
public int ID { get; set; }
[Column(Name="product_name")]
public string ProductName { get; set; }
[Column(Name="product_price")]
public string ProductPrice { get; set; }
[Column(Name="sale_date")]
public DateTime SaleDate { get; set; }
}
Thanks for the answers
I ended up sticking with my original approach as I wanted to keep the entities seperate from the data layer.
However to make it more manageable I made the context a partial class and created a file for each entity with a private method like MapUsers or MapRoles then in the OnModelCreating I just called these methods.
精彩评论