Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Stubs are generally only effective for <code>1-*</code> relationships. <code>*-*</code> relationships introduce a different set of challenges.</p> <p>Namely that when you attach both ends - unlike <code>1-*</code> - you still have no idea if the relationship already exists or not.</p> <p>So that means that this code:</p> <pre><code>if (issue.Scopes.Contains(thisScope)) </code></pre> <p>Is probably going to return false every time.</p> <p>What I would do is this:</p> <pre><code>edmx.Issues.Attach(issue); UpdateModel(issue); // or ctx.LoadProperty(issue, "Scopes") if it is a POCO class. issue.Scopes.Load(); // hit the database to load the current state. </code></pre> <p>Now you need to find out what you need to add &amp; remove from issue.Scopes. You can do this by comparing based on ID.</p> <p>i.e. if you have a set of Scope IDs you want to have related to the issue (relatedScopes)</p> <p>Then this code works out what to add and what to remove.</p> <pre><code>int[] toAdd = relatedScopes.Except(issue.Scopes.Select(s =&gt; s.ID)).ToArray(); int[] toRemove = issue.Scopes.Select(s =&gt; s.ID).Except(relatedScopes).ToArray(); </code></pre> <p>Now for toAdd you do this:</p> <pre><code>foreach(int id in toAdd) { var scope = new Scope{Id = id}; edmx.Scopes.Attach(scope); issue.Scopes.Add(scope); } </code></pre> <p>And for each scope you need to remove</p> <pre><code>foreach(int id in toRemove) { issue.Scopes.Remove(issue.Scopes.Single(s =&gt; s.ID == id)); } </code></pre> <p>By now the correct relationships should be formed.</p> <p>Hope this helps</p> <p><a href="http://twitter.com/adjames" rel="noreferrer">Alex</a></p> <p>Microsoft</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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