首先在实体框架中映射列

发布于 2024-11-08 11:23:43 字数 1768 浏览 0 评论 0原文

我在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还不是爱你 2024-11-15 11:23:43

我相信您只需要

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

对所有列进行操作,您希望数据库列名称的命名与其属性名称不同。


编辑:经过更多搜索后,我遇到了这个问题。从这一点和您发布的错误来看, mc.Properties() 似乎更多的是用于将值拆分到不同的表中,而不是实际重命名这些表名称。我认为重命名仍然需要手动完成。

这又是来自谷歌搜索的信息,我不知道我是否只是错过了一块来做你想要的:)。

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 :).

女中豪杰 2024-11-15 11:23:43

使用数据注释怎么样?

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

您可以在 http://msdn.microsoft.com/en- 找到包含示例的完整列表。 us/data/gg193958

第 3 方编辑

上面的链接重定向到实体框架 6 概述。您可能正在寻找

How about using DataAnnotations?

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文