使用EF Code First的多对多关系 [英] Many-to-many relationships using EF Code First

查看:442
本文介绍了使用EF Code First的多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类定义为:

public class Questionnaire
    {
        public int QuestionnaireID { get; set; }
        public string Title { get; set; }
        public bool Active { get; set; }
        public virtual ICollection<Question> Questions { get; set; }
        public virtual ICollection<Vendor> Vendors { get; set; }
    }

public class Vendor
    {
        public int VendorID { get; set; }

        public string VendorName { get; set; }

        public virtual ICollection<Questionnaire> OpenQuestionnaires { get; set; }

        public virtual ICollection<Questionnaire> SubmittedQuestionnaires { get; set; }

        public virtual ICollection<QuestionnaireUser> QuestionnaireUsers { get; set; }
    }

我觉得这是建立多对多的正确方法这些课程之间的关系,以及项目建成时,我会期望创建三个表。

I beleive this is the correct way to establish a many-to-many relationship between these classes, and when the project is built, I would expect three tables to be created.

但是,当我尝试将一个问卷与两个不同的供应商,尝试保存更改时会收到以下错误(context.SaveChanges()):

However, when I attempt to to relate one Questionnaire to two different Vendors, I receive the following error when attempting to save the changes (context.SaveChanges()):

*违反多重约束。关系的Vendor_OpenQuestionnaires_Source的角色QuestionnaireApp.Models.Vendor_OpenQuestionnaires具有多重性1或0..1。*

*Multiplicity constraint violated. The role 'Vendor_OpenQuestionnaires_Source' of the relationship 'QuestionnaireApp.Models.Vendor_OpenQuestionnaires' has multiplicity 1 or 0..1.*

如果我只向一个供应商分配问卷,保存更改,然后将其分配给另一个,并再次保存更改我不再收到错误;然而,问卷调查仅与最后一位供应商分配相关,表明(最多)存在一对多关系正在创建。

If I assign a Questionnaire to only one Vendor, save the changes and then assign it to another and again save changes I no longer get the error; however the Questionaire is then related only to the last Vendor to which it was assigned, indicating that (at best) there is a one-to-many relationship being created.

我希望我宣布这些类之间的多对多关系的方式有问题,或者也许有一些我需要添加到上下文类中以鼓励关系,但也许许多不支持这样的关系,或不能使用代码优先创建?

I'm hoping that there is something wrong with the way I'm declaring the many-to-many relationship between these classes, or perhaps there is something I need to add to the context class to "encourage" the relationsip, but perhaps many-to-many relationships like this are not supported, or cannot be created using "Code First"?

感谢您的时间,

Jason

推荐答案

去图,经过一个小时左右的搜索,我去找到在我发布问题后30秒回答。

Go figure, after an hour or so of searching, I go and find the exact answer 30 seconds after I post my question.

解决方案是将以下内容添加到上下文类中:

The solution was to add the following to the context class:

modelBuilder.Entity<Vendor>()
                .HasMany<Questionnaire>(x => x.OpenQuestionnaires)
                .WithMany(x => x.Vendors)
                .Map(x =>
                    {
                        x.MapLeftKey("vID");
                        x.MapRightKey("qID");
                        x.ToTable("VendorQuestionnaires");
                    });

我通过阅读这个Stack Overflow文章找到答案: EF Code First Many-to-Many not work

I found the answer by reading this Stack Overflow post: EF Code First Many-to-Many not working

这篇关于使用EF Code First的多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆