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
}
question from:https://stackoverflow.com/questions/6061994/mapping-columns-in-entity-framework-code-first