<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Entity Framework relations and entityKey</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Thu, 09 Feb 2012 16:15:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: Linq to entities insert problem &#8211; make sure the qualifiedEntitySetName is correct &#171; OnParSoftware</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/comment-page-1/#comment-2766</link>
		<dc:creator>Linq to entities insert problem &#8211; make sure the qualifiedEntitySetName is correct &#171; OnParSoftware</dc:creator>
		<pubDate>Wed, 04 Nov 2009 09:51:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/#comment-2766</guid>
		<description>[...] all I found the core place to be for my concerns and questions. The ADO.Net forum and then I found this post which really got me on the track.  Ended up solving all my problems with the following when [...]</description>
		<content:encoded><![CDATA[<p>[...] all I found the core place to be for my concerns and questions. The ADO.Net forum and then I found this post which really got me on the track.  Ended up solving all my problems with the following when [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ahmed Soliman</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/comment-page-1/#comment-2588</link>
		<dc:creator>Ahmed Soliman</dc:creator>
		<pubDate>Wed, 22 Jul 2009 04:16:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/#comment-2588</guid>
		<description>thanks for your code, it works for insert but for update, it updates all properties successfully but the parent not updated.


public void UpdateGame(Games game)
        {
            EntityKey key = null;
            object original = null;
            using (EntitiesContx contx = new EntitiesContx ())
            {
                try
                {
                    Games g = new Games();
                    g.GameName = game.GameName;
                    g.GameDescription = game.GameDescription;
                    g.GameId = game.GameId;
                    

                    EntityKey _relationshipKey = new EntityKey();
                    _relationshipKey.EntityContainerName = typeof(EntitiesContx).Name;
                    _relationshipKey.EntitySetName = typeof(Genre).Name;
                    _relationshipKey.EntityKeyValues = new EntityKeyMember[1] { new EntityKeyMember(&quot;GenreId&quot;, game.Genre.GenreId) };
                   
                    g.GenreReference = new EntityReference();
                    g.GenreReference.EntityKey = _relationshipKey;

                    
                    contx.AttachTo(typeof(Games).Name, g);

                    contx.SaveChanges();
                }
                catch (Exception ex)
                {

                    throw;
                }
            }

        }


any help</description>
		<content:encoded><![CDATA[<p>thanks for your code, it works for insert but for update, it updates all properties successfully but the parent not updated.</p>
<p>public void UpdateGame(Games game)<br />
        {<br />
            EntityKey key = null;<br />
            object original = null;<br />
            using (EntitiesContx contx = new EntitiesContx ())<br />
            {<br />
                try<br />
                {<br />
                    Games g = new Games();<br />
                    g.GameName = game.GameName;<br />
                    g.GameDescription = game.GameDescription;<br />
                    g.GameId = game.GameId;</p>
<p>                    EntityKey _relationshipKey = new EntityKey();<br />
                    _relationshipKey.EntityContainerName = typeof(EntitiesContx).Name;<br />
                    _relationshipKey.EntitySetName = typeof(Genre).Name;<br />
                    _relationshipKey.EntityKeyValues = new EntityKeyMember[1] { new EntityKeyMember(&#8220;GenreId&#8221;, game.Genre.GenreId) };</p>
<p>                    g.GenreReference = new EntityReference();<br />
                    g.GenreReference.EntityKey = _relationshipKey;</p>
<p>                    contx.AttachTo(typeof(Games).Name, g);</p>
<p>                    contx.SaveChanges();<br />
                }<br />
                catch (Exception ex)<br />
                {</p>
<p>                    throw;<br />
                }<br />
            }</p>
<p>        }</p>
<p>any help</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alkampfer</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/comment-page-1/#comment-2533</link>
		<dc:creator>alkampfer</dc:creator>
		<pubDate>Wed, 24 Jun 2009 10:14:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/#comment-2533</guid>
		<description>Thanks for the explanation. :)

Alk.</description>
		<content:encoded><![CDATA[<p>Thanks for the explanation. <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Alk.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael L Perry</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/comment-page-1/#comment-2532</link>
		<dc:creator>Michael L Perry</dc:creator>
		<pubDate>Wed, 24 Jun 2009 04:55:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/#comment-2532</guid>
		<description>The reason that your extension method is not part of the framework is that the first parameter to the EntityKey constructor is the entity set name, not the entity type name. By default (in 1.0) these are the same. In 4.0, the set will be automatically pluralized. But even in 1.0, it is possible to pluralize it yourself. You can even define multiple set of the same entity type. In these situations, your extension method will break.</description>
		<content:encoded><![CDATA[<p>The reason that your extension method is not part of the framework is that the first parameter to the EntityKey constructor is the entity set name, not the entity type name. By default (in 1.0) these are the same. In 4.0, the set will be automatically pluralized. But even in 1.0, it is possible to pluralize it yourself. You can even define multiple set of the same entity type. In these situations, your extension method will break.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bob Sullentrup</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/comment-page-1/#comment-2439</link>
		<dc:creator>Bob Sullentrup</dc:creator>
		<pubDate>Thu, 14 May 2009 18:42:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/#comment-2439</guid>
		<description>Let me relate my experience with this nasty error and point out the terrain chasing it will take you over leading to a 
tremendously simple solution. 

CompanyGroup is pretty simple. It has a name and it has a Company object. 

I started with this:

        public static void Add(CompanyGroup item)
        {
            try
            {
                using (Entities scope = new Entities())
                {
  			scope.AddToCompanyGroup(item);
                       scope.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                LogException(ex, item);
                throw;
            }     
	}   

And got this error:

{&quot;An entity object cannot be referenced by multiple instances of IEntityChangeTracker.&quot;}  

So, I added this between lines 6 and 7:

 			(IEntityWithChangeTracker)item).SetChangeTracker(null);

That rewarded me with:

{&quot;The object cannot be added to the ObjectStateManager because it already has an EntityKey. 
Use ObjectContext.Attach to attach an object that has an existing key.&quot;}

So I changed 

	scope.AddToCompanyGroup(item);

to	scope.Attach(item);

Now it complained about: 

{&quot;An object with a temporary EntityKey value cannot be attached to an object context.&quot;}

(beginning to sound like some of the girls I dated in my youth, but I digress)

So I made the entity key null (didn&#039;t work) and used the method to create new (didn&#039;t work, either)

Along the way, I got this error, too:

 {&quot;The source query for this EntityCollection or EntityReference cannot be returned when the related object is in either an 
added state or a detached state and was not originally retrieved using the NoTracking merge option.&quot;}


The Solution? 

Replace the core, lines 7 and 8, with:

                    CompanyGroup newcg = new CompanyGroup();
                    newcg.GroupName = item.GroupName;
                    newcg.Company = scope.Company.Where(c =&gt; c.CompanyID == item.Company.CompanyID).First();

		    scope.AddToCompanyGroup(newcg);
                    scope.SaveChanges();

Essentially, I took the data passed via &#039;item&#039;, and moved it to newly created object of the same type that introduces the same 
scope as the one used in the Add.</description>
		<content:encoded><![CDATA[<p>Let me relate my experience with this nasty error and point out the terrain chasing it will take you over leading to a<br />
tremendously simple solution. </p>
<p>CompanyGroup is pretty simple. It has a name and it has a Company object. </p>
<p>I started with this:</p>
<p>        public static void Add(CompanyGroup item)<br />
        {<br />
            try<br />
            {<br />
                using (Entities scope = new Entities())<br />
                {<br />
  			scope.AddToCompanyGroup(item);<br />
                       scope.SaveChanges();<br />
                }<br />
            }<br />
            catch (Exception ex)<br />
            {<br />
                LogException(ex, item);<br />
                throw;<br />
            }<br />
	}   </p>
<p>And got this error:</p>
<p>{&#8220;An entity object cannot be referenced by multiple instances of IEntityChangeTracker.&#8221;}  </p>
<p>So, I added this between lines 6 and 7:</p>
<p> 			(IEntityWithChangeTracker)item).SetChangeTracker(null);</p>
<p>That rewarded me with:</p>
<p>{&#8220;The object cannot be added to the ObjectStateManager because it already has an EntityKey.<br />
Use ObjectContext.Attach to attach an object that has an existing key.&#8221;}</p>
<p>So I changed </p>
<p>	scope.AddToCompanyGroup(item);</p>
<p>to	scope.Attach(item);</p>
<p>Now it complained about: </p>
<p>{&#8220;An object with a temporary EntityKey value cannot be attached to an object context.&#8221;}</p>
<p>(beginning to sound like some of the girls I dated in my youth, but I digress)</p>
<p>So I made the entity key null (didn&#8217;t work) and used the method to create new (didn&#8217;t work, either)</p>
<p>Along the way, I got this error, too:</p>
<p> {&#8220;The source query for this EntityCollection or EntityReference cannot be returned when the related object is in either an<br />
added state or a detached state and was not originally retrieved using the NoTracking merge option.&#8221;}</p>
<p>The Solution? </p>
<p>Replace the core, lines 7 and 8, with:</p>
<p>                    CompanyGroup newcg = new CompanyGroup();<br />
                    newcg.GroupName = item.GroupName;<br />
                    newcg.Company = scope.Company.Where(c =&gt; c.CompanyID == item.Company.CompanyID).First();</p>
<p>		    scope.AddToCompanyGroup(newcg);<br />
                    scope.SaveChanges();</p>
<p>Essentially, I took the data passed via &#8216;item&#8217;, and moved it to newly created object of the same type that introduces the same<br />
scope as the one used in the Add.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Extension method to add LoadByKey to EntityFrameworks context</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/comment-page-1/#comment-2084</link>
		<dc:creator>Extension method to add LoadByKey to EntityFrameworks context</dc:creator>
		<pubDate>Wed, 18 Feb 2009 11:29:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/02/18/entity-framework-relations-and-entitykey/#comment-2084</guid>
		<description>[...] Entity Framework relations and entityKey [...]</description>
		<content:encoded><![CDATA[<p>[...] Entity Framework relations and entityKey [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

