EF 4.1 双向一对一问题

发布于 2024-11-08 12:04:01 字数 1062 浏览 0 评论 0原文

您好,我在使用简单的 EF 4.1 代码优先模型时遇到问题。

我有一个班级人员和一个双向链接的班级调查。数据库模型是正确的,但我总是收到此错误:

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.

Class Person

[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 Survey

   [Key]
        public Guid Id { get; set; }

        public bool IsFinished { get; set; }

        public virtual Person Person { get; set; }

Datacontext:

 modelBuilder.Entity<Survey>().HasRequired(s => s.Person).WithOptional().WillCascadeOnDelete(true);

任何人都可以帮忙吗

Hi I am having a problem with a simple EF 4.1 code first model.

I have a class person and a class survey that are bidirectionally linked. The database model is correct but I always get this error:

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.

Class Person

[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 Survey

   [Key]
        public Guid Id { get; set; }

        public bool IsFinished { get; set; }

        public virtual Person Person { get; set; }

Datacontext:

 modelBuilder.Entity<Survey>().HasRequired(s => s.Person).WithOptional().WillCascadeOnDelete(true);

Can anyone help please

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岛徒 2024-11-15 12:04:01

您应该在映射中定义其他导航属性,因为模型中有它。否则 EF 将创建第二个(一对多)关联:

modelBuilder.Entity<Survey>()
            .HasRequired(s => s.Person)
            .WithOptional(p => p.Survey)
            .WillCascadeOnDelete(true);

You should define the other navigation property in your mapping since you have it in the model. Otherwise EF will create a second (one-to-many) association:

modelBuilder.Entity<Survey>()
            .HasRequired(s => s.Person)
            .WithOptional(p => p.Survey)
            .WillCascadeOnDelete(true);
森末i 2024-11-15 12:04:01

我认为您必须通过 HasForeignKey 指定外键属性或使用 Map 指定外键列名称。像这样的东西:

modelBuilder.Entity<Survey>()
    .HasRequired(s => s.Person)
    .WithOptional()
    .WillCascadeOnDelete(true)
    .Map(m => m.MapKey("fk_column"));

I think you have to specify either a foreign key property through HasForeignKey or foreign key column name using Map. Something like this:

modelBuilder.Entity<Survey>()
    .HasRequired(s => s.Person)
    .WithOptional()
    .WillCascadeOnDelete(true)
    .Map(m => m.MapKey("fk_column"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文