首先在实体框架代码中绘制列 [英] Mapping Columns in Entity Framework Code First
问题描述
我正在追踪一个教程它必须由CTP版本之一构建,因为某些方法缺少/不同。
我的模型看起来像:
public class Dinner
{
public int DinnerID {get;组; }
public string HostedBy {get;组; }
public DateTime EventDate {get;组; }
public string标题{get;组; }
public string Address {get;组; }
}
public class NerdDinners:DbContext
{
public DbSet< Dinner>晚餐{get;组;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//这是我尝试的,但它是失败
modelBuilder.Entity< Dinner>()。地图(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
我收到此错误:
属性表达式'c => new
<> f__AnonymousType0`5(colID =
c.DinnerID,colTitle = c.Title,
colHost = c.HostedBy,colDate =
c.EventDate,colAddress = c.Address)'
无效。表达式应该
表示一个属性:C#:'t =>
t.MyProperty'VB.Net:'Function(t)
t.MyProperty'。当指定
时,多个属性使用匿名
类型:C#:'t => new {t.MyProperty1,
t.MyProperty2}'VB.Net:'Function(t)
新的{t.MyProperty1,
t.MyProperty2}'。
映射列的正确语法是什么?如果您让我知道如何将地址属性映射到一个称为地址的子类中,那么Bonus Points?
public class Address
{
City
状态
Zip等
}
我相信你只需要做
modelBuilder.Entity< Dinner>()。Property(x => x.HostedBy).HasColumnName(colHost);对于您希望将数据库列名与其属性名称不同的所有列的
编辑:经过一些更多的搜索,我遇到了这个问题。从那里和你发布的错误,似乎像$ code> mc.Properties()更多的是将值分割成不同的表,而不是实际重命名这些表名。我认为重命名仍然需要手动进行。
这是Google搜索结果中的信息,我不知道我是否只是错过了一个准确的你要什么 :)。
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
}
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 :).
这篇关于首先在实体框架代码中绘制列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!