Note that there are some explanatory texts on larger screens.

plurals
  1. POMany-to-many relationships using EF Code First
    primarykey
    data
    text
    <p>I have two classes defined as such:</p> <pre><code>public class Questionnaire { public int QuestionnaireID { get; set; } public string Title { get; set; } public bool Active { get; set; } public virtual ICollection&lt;Question&gt; Questions { get; set; } public virtual ICollection&lt;Vendor&gt; Vendors { get; set; } } public class Vendor { public int VendorID { get; set; } public string VendorName { get; set; } public virtual ICollection&lt;Questionnaire&gt; OpenQuestionnaires { get; set; } public virtual ICollection&lt;Questionnaire&gt; SubmittedQuestionnaires { get; set; } public virtual ICollection&lt;QuestionnaireUser&gt; QuestionnaireUsers { get; set; } } </code></pre> <p>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.</p> <p>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()):</p> <p>*Multiplicity constraint violated. The role 'Vendor_OpenQuestionnaires_Source' of the relationship 'QuestionnaireApp.Models.Vendor_OpenQuestionnaires' has multiplicity 1 or 0..1.*</p> <p>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.</p> <p>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"?</p> <p>Thank you for your time,</p> <p>Jason</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload