【发布时间】:2011-05-14 13:11:59
【问题描述】:
我正在尝试对“用户可以有 0 或 1 组首选项”进行建模,其中首选项表的主键 UserId 也是用户实体的外键,例如 this post。
我希望我的模型类似于:
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int Id { get; set; }
[Required]
public virtual string Username { get; set; }
public virtual UserPreferences Preferences { get; set; }
}
public class UserPreferences
{
[Key]
public User User { get; set; }
public bool SubscribedToNewsLetter{ get; set; }
}
带配置:
HasOptional(u => u.Preferences).WithRequired(l => l.User);
产量:
SetUp : System.Data.Entity.ModelConfiguration.ModelValidationException : One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'UserPreferences' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �UserPreferences� is based on type �UserPreferences� that has no keys defined.
【问题讨论】:
标签: mapping entity-framework-4.1 code-first one-to-one