首先在实体框架中映射列
我在尝试将 EF 4.1 Code First 模型映射到数据库时遇到问题。当数据库与代码完全匹配时,一切工作正常,但是当我尝试在列名称不同时进行映射时,我就会遇到问题。
我正在遵循一个教程,该教程必须是使用 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
我收到此错误:
属性表达式 'c =>;新的 <>f__AnonymousType0`5(colID = c.DinnerID, colTitle = c.Title, colHost = c.HostedBy, colDate = c.EventDate, colAddress = c.Address)' 无效。表达式应该 代表一个属性:C#: 't => t.MyProperty' VB.Net: '函数(t) t.我的财产'。指定时 多个属性使用匿名 类型:C#: 't =>新的 { t.MyProperty1, t.MyProperty2 }' VB.Net: '函数(t) 新来自 { t.MyProperty1, t.MyProperty2 }'。
映射列的正确语法是什么?
如果您让我知道如何将 Address 属性映射到名为 Address 的子类中,我将获得奖励积分:
public class Address
{
City
State
Zip, etc
}
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您只需要
对所有列进行操作,您希望数据库列名称的命名与其属性名称不同。
编辑:经过更多搜索后,我遇到了这个问题。从这一点和您发布的错误来看, mc.Properties() 似乎更多的是用于将值拆分到不同的表中,而不是实际重命名这些表名称。我认为重命名仍然需要手动完成。
这又是来自谷歌搜索的信息,我不知道我是否只是错过了一块来做你想要的:)。
I believe you just have to do
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 :).
使用数据注释怎么样?
您可以在 http://msdn.microsoft.com/en- 找到包含示例的完整列表。 us/data/gg193958
第 3 方编辑
上面的链接重定向到实体框架 6 概述。您可能正在寻找
实体框架核心 - 实体属性实体属性映射到表列
实体框架 6 - 代码优先数据注释
How about using DataAnnotations?
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
Entity Framework Core - Entity Properties entity properties map to table columns
Entity Framework 6 - Code First Data Annotations