Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding new rows throws 'Multiplicity constraint violated'
    text
    copied!<p>I have two related classes like these : </p> <pre><code>public partial class WorkItem { public WorkItem() { this.ChildWorkItems = new HashSet&lt;WorkItem&gt;(); this.Attachments = new HashSet&lt;Attachment&gt;(); } public int Id { get; set; } public Nullable&lt;int&gt; ParentId { get; set; } public string Title { get; set; } public string SenderUserName { get; set; } public virtual ICollection&lt;WorkItem&gt; ChildWorkItems { get; set; } public virtual WorkItem ParentWorkItem { get; set; } public virtual ICollection&lt;Attachment&gt; Attachments { get; set; } } </code></pre> <p>and the realted 1-n attachments : </p> <pre><code>public partial class Attachment { public int Id { get; set; } public string Filename { get; set; } public Nullable&lt;int&gt; WorkItemId { get; set; } public virtual WorkItem WorkItem { get; set; } } </code></pre> <p>Now I want to insert some new <code>workitems</code> with their attachment into the database. I use the following code for insertion :</p> <p>at first put all workitems (regardless of their attachments )</p> <pre><code> var workItems = new List&lt;WorkItem&gt;(); foreach (var username in AllUsers) { var workitem = new WorkItem(); //fill the simple fields lst.Add(workitem); Context.WorkItems.Add(workitem); } </code></pre> <p>then set the attachments :</p> <pre><code> foreach (var fileName in MYFILES) { var file = new System.IO.FileInfo(fileName); foreach (var workItem in workItems) { var att = new Attachment() { Filename = file.Name, }; context.Attachments.Add(att); att.WorkItem = workItem; } } </code></pre> <p>But I get the following exception : </p> <p><strong>Multiplicity constraint violated. The role 'WorkItem' of the relationship 'AienCRMModel.FK_WorkItemAttachments_WorkItems' has multiplicity 1 or 0..1.</strong></p> <p>The interesting point is if I only have <strong>One</strong> <code>workitem</code> , everything is ok. If I have more than one <code>WorkItem</code> with no attachments , again everything is ok. The problem raises when having more than one WorkItem with at least one attachment. </p> <p>I read lots of other post but nothing usefuk was found. </p> <p>NOTE : I use EF Code-First 4.3 with a T4 template which generate my classes from the EDMX file. </p> <h2>I Really appreciate your helps in advance.</h2> <p><strong>EDIT</strong> I Attached the full EDMX diagram of mentioned tables here : <img src="https://i.stack.imgur.com/gKFhL.png" alt="EDMX diagram"></p> <p><strong>EDIT 2</strong> </p> <p>Complete .edmx file at here : <a href="http://www.4shared.com/file/zQUO_qk7/CrmModel141.html?" rel="nofollow noreferrer">http://www.4shared.com/file/zQUO_qk7/CrmModel141.html?</a></p>
 

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