1

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 the best way to do this?

2 Answers 2

3

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; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Although, if you are keeping your Business Entities separated from your data layer this doesn't seem like the best approach
1

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.

2 Comments

Interesting solution - using partials and then the Event. This means you could have 1 partial class per poco, right? (worse case scenario).
yes that's right. Maybe its not for everyone but for me it makes the code a lot more manageable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.