<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx</link><description>&amp;#160; The information in this post is out of date. Visit msdn.com/data/ef for the latest information on current and past releases of EF. For Add/Attach and Entity States see http://msdn.com/data/jj592676 &amp;#160; Introduction Version 4.1 of the Entity</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10399829</link><pubDate>Wed, 06 Mar 2013 09:41:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10399829</guid><dc:creator>Schukin Ilya</dc:creator><description>&lt;p&gt;That was a helpful post!&lt;/p&gt;
&lt;p&gt;Thank you. &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10399829" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10367570</link><pubDate>Sun, 11 Nov 2012 19:34:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10367570</guid><dc:creator>Michael</dc:creator><description>&lt;p&gt;Thanks for reminding me what Context.Entry did I&amp;#39;ve been perplexed wondering why my entities weren&amp;#39;t being saved!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10367570" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10260317</link><pubDate>Tue, 24 Jan 2012 23:08:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10260317</guid><dc:creator>Arthur</dc:creator><description>&lt;p&gt;@Kyralessa: To delete you can either query/Find and then delete or Attach a stub containing necessary information and then delete. Necessary information is always the primary key and then also any concurrency tokens if you have them. &lt;/p&gt;
&lt;p&gt;@Anon: This post describes why getting the object back from Add is useful: &lt;a rel="nofollow" target="_new" href="http://blog.oneunicorn.com/2011/04/19/ef-4-1-quick-tip-addattach-returns-your-entity/"&gt;blog.oneunicorn.com/.../ef-4-1-quick-tip-addattach-returns-your-entity&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;@tensai: I’m not sure what you mean by “loaded to the memory”. It means that Attach will essentially always attach the full graph of entities that you currently have loaded into memory and are reachable from the existing object. It will not load new objects into memory that are not already in memory.&lt;/p&gt;
&lt;p&gt;@JobLot You will need to use the change tracker API—context.ChangeTracker.Entries—to process the child entities and mark them as Modified if they need to be saved to the database. Note though that if the objects are already being tracked but have not changed then it is usually not required to save them to the database since doing so would just be writing the same data that is already there. If the objects have changed then DetectChanges (or equivalent) should detect this and mark the entities as Modified automatically.&lt;/p&gt;
&lt;p&gt;@JohnnyFee @Person: There is no need to Attach before setting the state to Modified. Setting the state to Modified will do the Attach first, if necessary. The example that shows Attach first is perhaps not clear. The idea is that if the entity is already tracked, which calling Attach sets up, then changing the state to Modified will change its state to Modified. Maybe this is too simple to even need an example. &lt;/p&gt;
&lt;p&gt;@Ralf: This shouldn’t happen if the entity that you got from the database is being tracked by the context. If you’re getting the entity in some way where it isn’t being tracked then you should Attach it first before making the relationship to the new entity.&lt;/p&gt;
&lt;p&gt;@Shimmy: I can see your post. If you still have a question, then please comment again.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Arthur&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10260317" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10259428</link><pubDate>Sun, 22 Jan 2012 20:58:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10259428</guid><dc:creator>Shimmy Weitzhandler</dc:creator><description>&lt;p&gt;Please help me with this issue:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://goo.gl/oDHVp"&gt;http://goo.gl/oDHVp&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I can&amp;#39;t attach values when using inherited tables, I see the following error:&lt;/p&gt;
&lt;p&gt;A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10259428" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10195129</link><pubDate>Fri, 12 Aug 2011 10:31:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10195129</guid><dc:creator>Ralf</dc:creator><description>&lt;p&gt;Hi!&lt;/p&gt;
&lt;p&gt;I have an issue with the Add() method. I create a new Object (in this expample it would be a unicorn) and then I get another one from the database (let&amp;#39;s say it&amp;#39;s a princess). Then I give the just created Unicorn the &amp;quot;old&amp;quot; existing princess.&lt;/p&gt;
&lt;p&gt;When I now add the unicorn to the context the princess is marked as added, too, which causes EF to try to insert it into the database again.&lt;/p&gt;
&lt;p&gt;Is there a way to prevent this without iterating over all related entities and manipulate the state manually?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10195129" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10186286</link><pubDate>Wed, 13 Jul 2011 22:25:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10186286</guid><dc:creator>Person</dc:creator><description>&lt;p&gt;JohnnyFee: &amp;nbsp;I think you are correct. &amp;nbsp;I was reading and it seems like the code for those two sections are in the wrong places. &amp;nbsp;They need to be switched, so the code sample matches the section description.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10186286" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10168915</link><pubDate>Fri, 27 May 2011 03:10:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10168915</guid><dc:creator>JohnnyFee</dc:creator><description>&lt;p&gt;On the header &amp;quot;Attaching an existing but modified entity to the context&amp;quot;:&lt;/p&gt;
&lt;p&gt;should we add the code &amp;quot; context.Unicorns.Attach(existingUnicorn);&amp;quot;&lt;/p&gt;
&lt;p&gt;before &amp;quot;context.Entry(existingUnicorn).State = EntityState.Modified;&amp;quot;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10168915" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10161252</link><pubDate>Thu, 05 May 2011 05:00:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10161252</guid><dc:creator>Matt Weber</dc:creator><description>&lt;p&gt;JobLot: I don&amp;#39;t have any hands-on experience with your particular scenario, but I believe that although the child entities get attached alongside the parent entity, the change state for all of the children is left at Unchanged. I think you would need to loop through all of the children and set the state to Modified.&lt;/p&gt;
&lt;p&gt;&amp;lt;a href=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://omniscientist.net&amp;quot;&amp;gt;http://omniscientist.net&amp;lt;/a&amp;gt;"&gt;http://omniscientist.net&amp;quot;&amp;gt;http://omniscientist.net&amp;lt;/a&amp;gt;&lt;/a&gt;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10161252" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10160998</link><pubDate>Wed, 04 May 2011 16:15:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10160998</guid><dc:creator>JobLot</dc:creator><description>&lt;p&gt;Attaching an existing but modified entity to the context using&lt;/p&gt;
&lt;p&gt;context.Entry(existingEntity).State = EntityState.Modified;&lt;/p&gt;
&lt;p&gt;does not update changes to collection of child entity&lt;/p&gt;
&lt;p&gt;I have a member entity which has collection on investment in it. a member can hv zero or many investments. When I try to update changes to member and one of their investments, the member info gets update, but the investment in not changed. Could someone please suggest why? Thanks&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10160998" width="1" height="1"&gt;</description></item><item><title>re: Using DbContext in EF 4.1 Part 4: Add/Attach and Entity States</title><link>http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx#10155527</link><pubDate>Tue, 19 Apr 2011 03:51:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10155527</guid><dc:creator>tensai</dc:creator><description>&lt;p&gt;Nice post.&lt;/p&gt;
&lt;p&gt;In the &amp;quot;Attaching an existing entity to the context&amp;quot; part, this statement can be read: &amp;quot;if the entity being attached has references to other entities that are not yet tracked then these new entities will also attached to the context in the Unchanged state&amp;quot;... Does this mean that if we attach an entity that has hundreds of thousands of references, all of these references will be loaded to the memory when attached? e.g. A service offer having hundreds of thousands of subscribers. &lt;/p&gt;
&lt;p&gt;Reply is appreciated. Thanks.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10155527" width="1" height="1"&gt;</description></item></channel></rss>