0

我有实体:

public class User
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        ...
        public virtual Profile Profile { get; set; }
        ...
public class Profile
    {
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

当我尝试获取用户时出现错误:

System.Data.Edm.EdmEntityType: : EntityType 'Profile' has no key defined. Define the key for this EntityType.

当我将实体更改为:

public class User
    {
        [Key]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        ...
        public virtual Profile Profile { get; set; }
        ...
public class Profile
    {
        [Key]
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

我得到错误:

{"Invalid column name 'Profile_UserId'."}

我究竟做错了什么?
在此处输入图像描述

4

1 回答 1

1

根据http://blog.bennymichielsen.be/2011/06/02/entity-framework-4-1-one-to-one-mapping/它可以工作:

public class User
{
        [Key]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public virtual Profile Profile { get; set; }
}

public class Profile
{
        [Key]
        public int UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [Required]
        public virtual User User { get; set;}
}
于 2013-02-22T21:12:14.720 回答