我在尝试将EF 4.1代码优先模型映射到数据库时遇到问题。当数据库完全匹配代码时,一切正常,但当我尝试和映射列名称不同时,然后我遇到问题。在实体框架代码中映射列首先
我在跟着一个必须用CTP构建之一构建的教程,因为有些方法缺失/不同。
我的模型看起来像:
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");
}
);
}
}
我希望我的表是:
tblDinners
colID
colHost
colDate
colTitle
colAddress
我收到此错误:
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 }'.
什么是正确的语法来图列?如果
的积分你让我知道如何将地址属性映射到一个子类,称为地址:
public class Address
{
City
State
Zip, etc
}
从教程,我下面就好像你可以一次全部映射多个列的完整列表,与样品。他们是否在最终版本中删除了这个功能? – Dismissile 2011-05-19 16:59:02
老实说,不确定,因为我还没有尝试重命名列。这是我亲眼看到的唯一方式,但我没有看到很多。然而,重新阅读你的问题中的代码,尝试从'.Map()'调用中取出'.ToTable()'调用并将它添加到Map的末尾(所以'.Map(mc => ..) ..)。ToTable()')。我不知道这在API中是否有效,但我不知道给映射的两个命令是否有效。 – KallDrexx 2011-05-19 17:10:07
尝试过之前的其他方式:) – Dismissile 2011-05-19 17:13:51