映射列在实体框架代码优先 [英] Mapping Columns in Entity Framework Code First

查看:127
本文介绍了映射列在实体框架代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在遇到麻烦想我的EF 4.1代码优先模型映射到数据库。一切工作正常,当数据库的代码完全匹配,但是当我尝试和地图时,列名不同,那么我遇到的问题。



我在下面的教程。那一定是建立在CTP之一,建立,因为一些方法缺少/不同的



我的模型是这样的:

 公共类晚餐
{
公众诠释DinnerID {搞定;组; }
公共字符串HostedBy {搞定;组; }
公众的DateTime EVENTDATE {搞定;组; }
公共字符串名称{搞定;组; }
公共字符串地址{搞定;组; }
}

公共类NerdDinners:的DbContext
{
公共DbSet<晚餐及GT;晚餐{搞定;组; }

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
//这是我尝试,但这种失败
modelBuilder.Entity<晚餐>()。地图(MC =>
{
mc.Properties(C =>新建{
colID = c.DinnerID,
colTitle = c.Title,
colHost = c.HostedBy,
colDate = c.EventDate,
colAddress = c.Address
});
mc.ToTable(tblDinner);
$} b $ b);
}
}



我希望我的表是:



  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.MyProperty。当指定
多个属性使用匿名
类:C#:'T =>新{t.MyProperty1,
t.MyProperty2}'VB.Net:'功能(T)
新从{t.MyProperty1,
t.MyProperty2}。




什么是正确的语法来映射列?



奖励积分,如果你让我知道如何将地址属性映射到一个子类,称为地址:

 公共类地址
{


邮编等
}


解决方案

我相信你只需要做

  modelBuilder.Entity<晚餐>()房产(X => x.HostedBy)。.HasColumnName(colHost); 



所有你想要的数据库列名不同于自己的财产的名字命名的列。






编辑:经过一番搜索,我碰到的this问题。从判断和您发布的错误,它似乎像 mc.Properties()则多为分割值到不同的表,而不是实际重命名这些表名。我觉得重命名仍然必须手动的基础上完成的。



这又是从谷歌上搜索信息,我不知道如果我只是缺了一块做的正是你要什么 :)。


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆