如何使用实体框架4 code首先声明一对一的关系(POCO) [英] How to declare one to one relationship using Entity Framework 4 Code First (POCO)

查看:100
本文介绍了如何使用实体框架4 code首先声明一对一的关系(POCO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用实体框架4 code首先(POCO)申报一对一的关系?

How to declare a one to one relationship using Entity Framework 4 Code First (POCO)?

我发现这个问题,<一个href=\"http://stackoverflow.com/questions/2089395/one-to-one-relationships-in-entity-framework-4-v2-with-poco\">http://stackoverflow.com/questions/2089395/one-to-one-relationships-in-entity-framework-4-v2-with-poco,但是,答案引用不实用的文章(有code的一条线是一个1-1的关系,但没有提及如何定义它)。

I found this question http://stackoverflow.com/questions/2089395/one-to-one-relationships-in-entity-framework-4-v2-with-poco, but the article that the answer references was not useful (there is one line of code that is a 1-1 relationship, but no mention of how to define it).

推荐答案

您只是在寻找这样的事情?

Are you just looking for something like this?

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public Profile Profile { get; set; }
    public int ProfileId { get; set; }
}

public class Profile
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PostalCode { get; set; }
    // etc...
}

public class UserMapping : EntityConfiguration<User>
{
    public UserMapping()
    {
        this.HasKey(u => u.Id);
        this.Property(u => u.Username).HasMaxLength(32);

        // User has ONE profile.
        this.HasRequired(u => u.Profile);
    }
}

public class ProfileMapping : EntityConfiguration<Profile>
{
    public ProfileMapping()
    {
        this.HasKey(p => p.Id);
        this.Property(p => p.FirstName).HasMaxLength(32);
        this.Property(p => p.LastName).HasMaxLength(32);
        this.Property(p => p.PostalCode).HasMaxLength(6);
    }
}

修改:是的,我没有VS在我面前,但你需要添加下面的行,而不是 UserMapping 的电流 HasRequired 键,还添加了配置文件ID 属性(而不是 PROFILE_ID 你添加的):

EDIT: Yeah I didn't have VS in front of me but you need to add the following line in the UserMapping instead of the current HasRequired and also add a ProfileId property (instead of Profile_Id that you added):

this.HasRequired(u => u.Profile).HasConstraint((u, p) => u.ProfileId == p.Id);

目前,我不认为有办法解决,但我敢肯定它会改变,因为我们只是在CTP4。这会是很好,如果我可以说:

I currently don't think there's a way around this, but I'm sure it'll change since we're only in CTP4. It'd be nice if I could say:

this.HasRequired(u => u.Profile).WithSingle().Map(
    new StoreForeignKeyName("ProfileId"));

这样我就不必包含配置文件ID 属性。也许有解决这个目前的一种方法,它仍然在早上早,我想:)。

This way I wouldn't have to include a ProfileId property. Maybe there is a way around this currently and it's still to early in the morning for me to think :).

还记得叫 .INCLUDE(档案)如果要包含一个导航属性。

Also remember to call .Include("Profile") if you want to include a "navigational property".

这篇关于如何使用实体框架4 code首先声明一对一的关系(POCO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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