【发布时间】:2012-09-24 13:02:45
【问题描述】:
我需要配置与以下类的关系: 用户有或没有个人资料。结构: 用户:用户ID、用户名、密码 个人资料:用户 ID、全名、地址、电话
public class User
{
#region Feilds
public int UserID { get; set; }
public string Email { get; set; }
public string Password { get; set; }
#endregion
public virtual Profile Profile { get; set; }
}
public class Profile
{
#region Fields
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
#endregion
public virtual User User { get; set; }
}
配置:
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
: base()
{
// Primary Key
this.HasKey(p => p.UserID);
//Foreign Key
this.HasOptional(p => p.Profile).
WithMany().
HasForeignKey(p => p.UserID);
}
}
错误:
System.Data.Edm.EdmEntityType: : EntityType 'Profile' 没有定义键。定义此 EntityType 的键。 System.Data.Edm.EdmEntitySet: EntityType: EntitySet “Profiles” 基于未定义键的“Profile”类型。
请帮忙。
谢谢。
【问题讨论】:
标签: asp.net-mvc-3 entity-framework c#-4.0 ef-code-first