我有实体:
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'."}
我做错了什么?
答案 0 :(得分: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;}
}