c# - EF 4.1 双向一对一问题

标签 c# model-view-controller entity-framework-4 ef-code-first

您好,我在使用简单的 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/

相关文章:

c# - 更新母版页计时器上的内容页控件 TIck

c# - 通过 for 循环迭代文本框

java - MVC模式;我的模型类需要用户输入,但我知道它不应该询问用户或 Controller

oracle - 使用现有存储过程通过 Entity Framework 为 Oracle 数据库生成身份

javascript - 如何使这个 JavaScript 弹出窗口模式化?

c# - 在 winforms 中显示 OpenTk.GLControl

java - 更新 Java Swing 应用程序中的按钮状态

ruby-on-rails - 使用实例变量或帮助程序管理用户权限

entity-framework - Entity Framework 合并选项不跟踪不良性能

c# - 动态 LINQ 查询中 DateTime.AddMinutes() 的问题