使用映射CF实体框架一比一的关系 [英] One-to-one relationship in CF Entity Framework using mapping

查看:211
本文介绍了使用映射CF实体框架一比一的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架5,code-第一。

I'm using Entity Framework 5, Code-First.

我有两个域对象(或表)。第一个是用户,和2是的用户配置即可。一个用户只能有一个配置文件,一个配置文件只能属于一个用户。这就是1-1的关系。

I've two domain objects (or tables). 1st is User, and 2nd is UserProfile. One user can have only one profile, and one profile belongs to only one user. That is 1-1 relationship.

下面是类....(我简化了code,使之可以理解的,它实际上是更复杂)

Here are the classes.... (I simplified the code to make it understandably, It is actually more complex)

用户

public class User {
    public virtual Int64 UserId { get; set; }
    public virtual UserProfile UserProfile { get; set; }
    public virtual String Username{ get; set; }
    public virtual String Email { get; set; }
    public virtual String Password { get; set; }
}

用户配置

public class UserProfile {
    public virtual Int64 UserId { get; set; }
    public virtual User User { get; set; }
    public virtual Int64 Reputation { get; set; }
    public virtual String WebsiteUrl { get; set; }
}

下面是地图......

Here are the Maps....

中userMap

public UserMap() {
    this.Property(t => t.Email)
        .IsRequired()
        .HasMaxLength(100);
    this.Property(t => t.Password)
        .IsRequired()
        .HasMaxLength(15);
    this.Property(t => t.Username)
        .IsRequired()
        .HasMaxLength(15);
}

UserProfileMap

public UserProfileMap()
    {
        this.HasKey(t => t.UserId);
    }

下面是上下文....

Here is the Context....

public class TcContext : DbContext {
    static TcContext () {
        Database.SetInitializer(new TcContextInitializer());
    }
    public DbSet<User> Users { get; set; }
    public DbSet<UserProfile> UserProfiles { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        modelBuilder.Configurations.Add(new UserMap());
        modelBuilder.Configurations.Add(new UserProfileMap());
    }
}

这是我的错误信息......

Unable to determine the principal end of an association between the types 'Tc.Domain.UserProfile' and 'Tc.Domain.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

我觉得这样EF应自动确定了恋爱关系。但它给我上面的错误消息。我研究这个问题一段时间,但在我的情况不能找到问题的一个很好的例证。

I think in this way EF should determine the relationship automatically. But it gives me the above error message. I've researched this problem for a while but can't find a good illustration of the problem in my case.

哪里是我的错?或者,我应该定义一些额外的关系的地图?

Where is my mistake? Or, should I define some sort of additional relations in maps?

推荐答案

我不得不修改中userMap 类如下:

public UserMap() {
    this.Property(t => t.Email)
        .IsRequired()
        .HasMaxLength(100);
    this.Property(t => t.Password)
        .IsRequired()
        .HasMaxLength(15);
    this.Property(t => t.Username)
        .IsRequired()
        .HasMaxLength(15);
    this.HasOptional(t => t.UserProfile)
        .WithRequired(t => t.User);
}

它本质上说:
    用户有一个可选的实体,用户配置这反过来有需要的用户实体

It essentially says: "User has an optional entity UserProfile which in turn has a required User entity"

用户配置的关键的定义是必须在这里。

The definition of the key in UserProfile is a must here.

这篇关于使用映射CF实体框架一比一的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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