Entity Framework Code First: Column Mapping

c# ef-code-first entity-framework

Question

My attempt to map my EF 4.1 Code First model to a database is failing. When the database and code match perfectly, everything runs well, however when I attempt to map when the columns' names vary, problems arise.

Because some of the methods are missing or altered, the tutorial I was following must have been created using one of the CTP versions.

My model seems to be:

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");
            }
        );
    }
}

My ideal table would be:

tblDinners  
    colID  
    colHost  
    colDate  
    colTitle  
    colAddress  

I'm receiving the following 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 syntax should be used to map the columns?

If you can tell me how to map the Address Property into a subclass named Address, bonus points will be awarded:

public class Address
{
     City
     State
     Zip, etc
}
1
37
10/10/2014 7:16:28 PM

Accepted Answer

I think all you have to do

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

You want the database column names for each of your columns to be distinct from their property names.


Edit: After doing some more research, I discovered this inquiry. Based on that and the mistake you posted, it seems that themc.Properties() is not really intended to rename those table names, but rather to separate values into various tables. Renaming, in my opinion, will still need to be done manually.

Again, I found this information via searching, so I'm not sure whether I'm simply lacking anything that would enable me to accomplish your goal exactly:).

52
5/23/2017 12:02:17 PM

Popular Answer

Consider using DataAnnotations.

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

At http://msdn.microsoft.com/en-us/data/gg193958, you may find a complete list with examples.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow