实体框架| code首先|从CultureInfo.Name SUB房产测绘 [英] Entity Framework | Code First | Mapping of sub property from CultureInfo.Name

查看:123
本文介绍了实体框架| code首先|从CultureInfo.Name SUB房产测绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体是这样的:

I've got an entity like this :

public class Course {
    public CultureInfo Culture {get; set;}
    :
}

和我想的CultureInfo 名称属性映射到表中的一列的实体框架生成适合我。

And I want to map just the Name property of CultureInfo to a single column in the table that Entity Framework generates for me.

我会如何呢?目前,我试着看的DbContext的OnModelCreating()方法,但我没有找到任何东西,是站在了。

How would I go about this? Currently I've tried looking at the OnModelCreating() method of the DbContext, but I'm not finding anything that is standing out.

我使用实体框架4.1和MVC 3应用程序与code第一种方法。

I'm using Entity Framework 4.1 in and MVC 3 application with a Code First approach.

非常感谢。

推荐答案

Unfrotunatelly EF不能做这种方式。解决方法是定义单独的属性与名称将被映射到数据库,并标记你的文化财产没有映射

Unfrotunatelly EF can't do it this way. The workaround is defining separate property with the Name which will be mapped to the database and marking your Culture property as not mapped.

public class Course
{
    public CultureInfo Culture { get; set; }
    public string CultureName 
    {
        get 
        {
            if (Cuture != null)
            {
                return Culture.Name;
            }

            return null;
        }
        set
        {
            // same check for value can be placed here

            Culture = new CultureInfo(value);
        }
    }
}

和映射将定义:

modelBuilder.Entity<Course>()
            .Ignore(c => c.Culture);

这篇关于实体框架| code首先|从CultureInfo.Name SUB房产测绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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