您好,我在使用简单的 EF 4.1 代码优先模型时遇到问题。
我有一个双向链接的类(class)人员和类(class)调查。数据库模型是正确的,但我总是得到这个错误:
Unable to determine the principal end of an association between the types 'DAL.Models.Survey' and 'DAL.Models.Person'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
类人
[Key]
public Guid Id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
public virtual Survey Survey { get; set; }
类(class)调查
[Key]
public Guid Id { get; set; }
public bool IsFinished { get; set; }
public virtual Person Person { get; set; }
数据上下文:
modelBuilder.Entity<Survey>().HasRequired(s => s.Person).WithOptional().WillCascadeOnDelete(true);
谁能帮忙吗
最佳答案
您应该在映射中定义其他导航属性,因为您在模型中有它。否则 EF 将创建第二个(一对多)关联:
modelBuilder.Entity<Survey>()
.HasRequired(s => s.Person)
.WithOptional(p => p.Survey)
.WillCascadeOnDelete(true);
关于c# - EF 4.1 双向一对一问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6072053/