开发者

Entity Framework Code First CTP5 Mapping

开发者 https://www.devze.com 2023-01-30 11:56 出处:网络
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 i

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消