<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : SaveChangesAsync - entity with it&#039;s related entities</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2010 : SaveChangesAsync - entity with it&#039;s related entities</description>
  <pubDate>Wed, 13 May 2026 04:53:03 -700</pubDate>
  <lastBuildDate>Thu, 21 Jun 2012 00:37:55 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=3497</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>SaveChangesAsync - entity with it&#039;s related entities : It was a save interceptor problem...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13883#13883</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1471" rel="nofollow">redman</a><br /><strong>Subject:</strong> 3497<br /><strong>Posted:</strong> 21-Jun-2012 at 12:37am<br /><br />It was a save interceptor problem :)]]>
   </description>
   <pubDate>Thu, 21 Jun 2012 00:37:55 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13883#13883</guid>
  </item> 
  <item>
   <title>SaveChangesAsync - entity with it&#039;s related entities :   redman,If you are doing a...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13845#13845</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=892" rel="nofollow">sbelini</a><br /><strong>Subject:</strong> 3497<br /><strong>Posted:</strong> 15-Jun-2012 at 12:38pm<br /><br />redman,<div>&nbsp;</div><div>If you are doing a partial save, you will need to include all entities to be saved. What I was initially referring to was that you are deliberately changing EntityAspect from Detached to Added. You do not need to to that as DevForce will automatically update the EntityState if you associate entity A (EntityState.Detached) to entity B (EntityState.Added/Unchanged).</div><div>&nbsp;</div><div>&nbsp;</div><div>Let's go back a bit... you mentioned your code <em>'fails with error "The INSERT statement conflicted with the FOREIGN KEY constraint ...."'</em>. </div><div>&nbsp;</div><div>I need to know what are the 2 entity type names you are saving AND the complete error message. (i.e. including the types involved in the constraint)</div><div>&nbsp;</div><div>Regards,</div><div>&nbsp;&nbsp; Silvio.</div>]]>
   </description>
   <pubDate>Fri, 15 Jun 2012 12:38:58 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13845#13845</guid>
  </item> 
  <item>
   <title>SaveChangesAsync - entity with it&#039;s related entities :   So you say that this should...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13844#13844</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1471" rel="nofollow">redman</a><br /><strong>Subject:</strong> 3497<br /><strong>Posted:</strong> 14-Jun-2012 at 11:19pm<br /><br />So you say that this should save related entities too?<div><br></div><div><table width="99%"><tr><td><pre class="BBcode"><span style=": rgb251, 251, 253; text-align: left; ">EntityManager.SaveChangesAsync(new List&lt;Entity&gt;(){ MyParentEntitie }, ....)</span></pre></td></tr></table></div><div><br></div><div>Based on this answer it don't -&nbsp;<a href="http://www.ideablade.com/forum/forum_posts.asp?TID=2802&amp;title=savechangesasync-and-associated-entities" target="_blank">http://www.ideablade.com/forum/forum_posts.asp?TID=2802&amp;title=savechangesasync-and-associated-entities</a></div><div><br></div><div>If I don't add related entities to list of entities that should be saved it fails with:</div><div><br></div><div><table width="99%"><tr><td><pre class="BBcode">An entity or entities containing a referenced temporary Id is missing from the list of entities provided. Missing entities include: MyEntityType: -101. &nbsp;See exception members for more details</pre></td></tr></table></div><div><br></div><div>But when I add them manually it inserts them in DB and still throws error (that's interesting, because it actually inserts relations into DB) on FK.</div><div><br></div><div>I removed FK and it goes like this:</div><div>It first saves ParentEntity ok, and then it saves related entities, but it tries twice for each. First (I guess it's first, because it should fail otherwise on first error) it saves with the right foreign key, and then tries to save it again with -100 for foreign key (this is internal for ParentEntity). Now I have two questions:</div><div>1. Why it tries to save it twice?</div><div>2. Shouldn't that SaveChanges work in a transaction? How can than happen that something is saved when error occurrs?</div><div><br></div><div>Edit: the above only happens when adding, when updating it works like expected</div><span style="font-size:10px"><br /><br />Edited by redman - 15-Jun-2012 at 12:28am</span>]]>
   </description>
   <pubDate>Thu, 14 Jun 2012 23:19:30 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13844#13844</guid>
  </item> 
  <item>
   <title>SaveChangesAsync - entity with it&#039;s related entities :     Hi redman,First of all,...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13841#13841</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=892" rel="nofollow">sbelini</a><br /><strong>Subject:</strong> 3497<br /><strong>Posted:</strong> 14-Jun-2012 at 2:42pm<br /><br />Hi redman,<div>&nbsp;</div><div>First of all, I assume you are writing this code as&nbsp;a form of updating the many-to-many related entities once they are "linked" to the existing entity (i.e. <em>Model</em>).</div><div>Note that DevForce should be doing this automatically for you. (assuming your <em>Model </em>entity is Added or Unchanged)</div><div>&nbsp;</div><div>Back to your code, I don't see anything wrong with it. (besides <em><font color="#0000ff" size="2" face="C&#111;nsolas"><font color="#0000ff" size="2" face="C&#111;nsolas"><font color="#0000ff" size="2" face="C&#111;nsolas">dynamic </font></font></font><font size="2" face="C&#111;nsolas"><font size="2" face="C&#111;nsolas">pval = Model.GetPropertyValue(prop.Name);</font></font></em> - where I use <em><font color="#0000ff" size="2" face="C&#111;nsolas"><font color="#0000ff" size="2" face="C&#111;nsolas"><font color="#0000ff" size="2" face="C&#111;nsolas">dynamic</font></font></font><font size="2" face="C&#111;nsolas"><font size="2" face="C&#111;nsolas"> pval = entity.EntityAspect.GetValue(prop.Name)</font></font></em> instead)</div><div><div>&nbsp;</div><div>While I was testing it (using Employee/Territory many-to-many relation fom NorthwindIB) I got the same error, but the issue was not between Employee and Territory. The issue was between Territory and Region as their DB constraint was not being satisfied. </div><div>&nbsp;</div><div>I'm &nbsp;guessing your issue is the same and the constraint is probably not between the 2 entity types being saved.</div><div>&nbsp;</div><div>Let me know.</div><div>&nbsp;</div><div>Regards,</div><div>&nbsp;&nbsp; Silvio.</div></div><span style="font-size:10px"><br /><br />Edited by sbelini - 14-Jun-2012 at 3:07pm</span>]]>
   </description>
   <pubDate>Thu, 14 Jun 2012 14:42:29 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13841#13841</guid>
  </item> 
  <item>
   <title>SaveChangesAsync - entity with it&#039;s related entities :  Hi,I have an entity that has...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13834#13834</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1471" rel="nofollow">redman</a><br /><strong>Subject:</strong> 3497<br /><strong>Posted:</strong> 14-Jun-2012 at 5:41am<br /><br />Hi,<div><br></div><div>I have an entity that has some many to many relationships. I get related entities like this:</div><div><br></div><div><div><table width="99%"><tr><td><pre class="BBcode">private IEnumerable&lt;Entity&gt; GetRelatedEntities()</div><div>{</div><div>&nbsp; &nbsp; &nbsp; &nbsp;//Model = this is an entity</div><div><br></div><div><span ="apple-tab-span"="" style="white-space:pre">	</span>var entities = new List&lt;Entity&gt;() { Model };</div><div><span ="apple-tab-span"="" style="white-space:pre">	</span>var t = Model.GetType();</div><div><span ="apple-tab-span"="" style="white-space:pre">	</span>var props = t.GetProperties().Where(p =&gt;</div><div><span ="apple-tab-span"="" style="white-space:pre">										</span> &nbsp; {</div><div><span ="apple-tab-span"="" style="white-space:pre">											</span> &nbsp; var ptype = p.PropertyType;</div><div><br></div><div><span ="apple-tab-span"="" style="white-space:pre">											</span> &nbsp; if (!ptype.IsGenericType) return false;</div><div><br></div><div><span ="apple-tab-span"="" style="white-space:pre">												</span>var gtype = ptype.GetGenericTypeDefinition();</div><div><span ="apple-tab-span"="" style="white-space:pre">												</span>if (gtype == typeof(RelatedEntityList&lt;&gt;))</div><div><span ="apple-tab-span"="" style="white-space:pre">												</span>{</div><div><span ="apple-tab-span"="" style="white-space:pre">													</span>return true;</div><div><span ="apple-tab-span"="" style="white-space:pre">												</span>}</div><div><br></div><div><span ="apple-tab-span"="" style="white-space:pre">											</span> &nbsp; return false;</div><div><span ="apple-tab-span"="" style="white-space:pre">										</span> &nbsp; }).ToList();</div><div><br></div><div><span ="apple-tab-span"="" style="white-space:pre">	</span>foreach (var prop in props)</div><div><span ="apple-tab-span"="" style="white-space:pre">	</span>{</div><div><span ="apple-tab-span"="" style="white-space:pre">		</span>dynamic pval = Model.GetPropertyValue(prop.Name);</div><div><br></div><div><span ="apple-tab-span"="" style="white-space:pre">		</span>foreach (dynamic e in pval)</div><div><span ="apple-tab-span"="" style="white-space:pre">		</span>{</div><div><span ="apple-tab-span"="" style="white-space:pre">			</span>if (e.EntityAspect.EntityState == EntityState.Detached)</div><div><span ="apple-tab-span"="" style="white-space:pre">			</span>{</div><div><span ="apple-tab-span"="" style="white-space:pre">				</span>DomainContext.AddEntity(e);</div><div><span ="apple-tab-span"="" style="white-space:pre">			</span>}</div><div><span ="apple-tab-span"="" style="white-space:pre">			</span>entities.Add(e);</div><div><span ="apple-tab-span"="" style="white-space:pre">		</span>}</div><div><span ="apple-tab-span"="" style="white-space:pre">	</span>}</div><div><br></div><div><span ="apple-tab-span"="" style="white-space:pre">	</span>return entities;</div><div>}</pre></td></tr></table></div></div><div><br></div><div>But when I save this list (EntityManager.SaveChangesAsync(GetRelatedEntities(),....) it fails with error "The INSERT statement conflicted with the FOREIGN KEY constraint ....". What I'm doing wrong?</div><div><br></div><div>p.s.: this is done in a ViewModelBase which is a generic class. I can't just do EntityRelations.XYZ and then FindEntityGraph, I have to do this at runtime, for "unknown" entity.</div><span style="font-size:10px"><br /><br />Edited by redman - 14-Jun-2012 at 5:46am</span>]]>
   </description>
   <pubDate>Thu, 14 Jun 2012 05:41:02 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=3497&amp;PID=13834#13834</guid>
  </item> 
 </channel>
</rss>