48

I'm having trouble trying to map my EF 4.1 Code First model to a database. Everything works fine when the database match the code exactly, but when I try and map when the columns differ in name then I am running into issues.

I was following a tutorial that must've been built with one of the CTP builds because some of the methods are missing/different.

My model looks like:

public class Dinner
{
    public int DinnerID { get; set; }  
    public string HostedBy { get; set; }
    public DateTime EventDate { get; set; }
    public string Title { get; set; }
    public string Address { get; set; }
}

public class NerdDinners : DbContext
{
    public DbSet<Dinner> Dinners { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // THIS IS WHAT I TRIED BUT IT IS FAILING
        modelBuilder.Entity<Dinner>().Map(mc =>
            {
                mc.Properties(c => new {
                    colID = c.DinnerID,
                    colTitle = c.Title,
                    colHost = c.HostedBy,
                    colDate = c.EventDate,
                    colAddress = c.Address
                });
                mc.ToTable("tblDinner");
            }
        );
    }
}

I want my table to be:

tblDinners  
    colID  
    colHost  
    colDate  
    colTitle  
    colAddress  

I am getting this error:

The properties expression 'c => new <>f__AnonymousType0`5(colID = c.DinnerID, colTitle = c.Title, colHost = c.HostedBy, colDate = c.EventDate, colAddress = c.Address)' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New From { t.MyProperty1, t.MyProperty2 }'.

What is the proper syntax to map the columns?

Bonus Points if you let me know how to map the Address Property into a subclass called Address:

public class Address
{
     City
     State
     Zip, etc
}

2 Answers 2

59

I believe you just have to do

modelBuilder.Entity<Dinner>().Property(x => x.HostedBy).HasColumnName("colHost");

for all of your columns that you want the db column names to be named differently than their property names.


Edit: After some more searching I came across this question. Judging from that and the error you posted, it seems like the mc.Properties() is more for splitting values into different tables, not to actually rename those table names. I think renaming will still have to be done on a manual basis.

This is again information from googling and I have no idea if I am just missing a piece to do exactly what you want :).

Sign up to request clarification or add additional context in comments.

3 Comments

From the tutorial I was following it seemed like you could map multiple columns all at once. Did they remove this functionality in the final build?
Honestly, not sure as I haven't tried to rename columns. This is the only way I have personally seen to go about it, but I haven't looked a great deal. However, re-reading the code in your question, try taking the .ToTable() call out of the .Map() call and add it to the end of Map (so .Map(mc => ....).ToTable()). I don't know if that's valid in the API, but I do wonder if giving two commands to mapping is valid.
Tried it the other way before :)
59

How about using DataAnnotations?

[Key]
[Column("ColID", TypeName="int")]
public int DinnerID { get; set; }  

You can find a full list with samples at http://msdn.microsoft.com/en-us/data/gg193958

3rd party edit

The link above redirects to Entity Framework 6 overview. You are probably looking for

6 Comments

It works but I'd rather not include this in the POCO classes.
I don't think it has any bad influence on you POCO classes. Also, I don't think it's against separation of concerns in any way or breaks any design pattern. You can do your validations very easily, and in my opinion, it's very reasonable. But that's just my idea...
I disagree. Your POCO classes shouldn't have to have any knowledge about the database schema, even if it is just metadata via attributes.
Depends on you requirements. It's not an absolute RULE. If you plan on supporting multiple databases, then you're right (for now. until EF Code-First supports other RDBMSs.) Otherwise, it effectively reduces your development time.
If you're using Attributes, you're doing it wrong. Period.
|

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.