Insert order problem when there is no deterministic order, helpHello everyone,<br/><br/>I have this table in my DB:<br/><br/><strong>Person</strong><br/>PersonID uniqueidentifier PK<br/>Name      varchar(100)<br/>ID           int IDENTITY (1,1)<br/><br/><br/>My problem is that I do the following: <br/><br/><span style="font-family:Courier New">context.Add(<span style="font-family:Courier New">New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;1st&quot;}</span>);<br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;2nd&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;3rd&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;4th&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;5th&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;6th&quot;});</span><br/>context.SaveChanges();</span><br/><br/>ID column of Person entity is Auto Increment in database.<br/><strong>I would expect to have this result</strong> (taken for granted that table is brand new)...<br/><span style="font-family:Courier New">ID | Name<br/>==========<br/>1  | 1st<br/>2  | 2nd<br/>3  | 3rd<br/>4  | 4th<br/>5  | 5th<br/>6  | 6th</span><br/><br/><strong>However I get this!...</strong><br/><span style="font-family:Courier New">ID | Name<br/>==========<br/>1  | 3rd<br/>2  | 2nd<br/>3<span style="font-family:Courier New">  |</span> 1st<br/>4<span style="font-family:Courier New">  |</span> 6th<br/>5<span style="font-family:Courier New">  |</span> 5th<br/>6<span style="font-family:Courier New">  |</span> 4th</span><br/><br/>So after reading this: <a href="http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d8448144-05dd-4c34-b93c-9336c3b62f6e/">http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d8448144-05dd-4c34-b93c-9336c3b62f6e/</a><br/><br/>I tried with Colin Meek's advice:<br/><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;1st&quot;}, ID=1);<br/>context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;2nd&quot;}, ID=2);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;3rd&quot;}, ID=3);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;4th&quot;}, ID=4);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;5th&quot;}, ID=5);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;6th&quot;}, ID=6);</span><br/><span style="font-family:Courier New">context.SaveChanges();<br/><br/><br/><strong><span style="font-family:Verdana">However I get this!...<br/></span></strong><span style="font-family:Courier New">ID | Name<br/>==========<br/>1  | 1st<br/>2  | 2nd<br/>3<span style="font-family:Courier New">  |</span> 6th<br/>4<span style="font-family:Courier New">  |</span> 3th<br/>5<span style="font-family:Courier New">  |</span> 5th<br/>6<span style="font-family:Courier New">  |</span> 4th</span><br/><br/>Why is not working? What is going on? Is there anyway I can get this to insert in the right order?<br/><br/>Thanks</span>© 2009 Microsoft Corporation. All rights reserved.Fri, 03 Jul 2009 04:43:27 Zbd6db5b9-ba1c-4859-8867-8fed183d5ecehttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#bd6db5b9-ba1c-4859-8867-8fed183d5ecehttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#bd6db5b9-ba1c-4859-8867-8fed183d5eceFederico Silberberghttp://social.msdn.microsoft.com/Profile/en-US/?user=Federico%20SilberbergInsert order problem when there is no deterministic order, helpHello everyone,<br/><br/>I have this table in my DB:<br/><br/><strong>Person</strong><br/>PersonID uniqueidentifier PK<br/>Name      varchar(100)<br/>ID           int IDENTITY (1,1)<br/><br/><br/>My problem is that I do the following: <br/><br/><span style="font-family:Courier New">context.Add(<span style="font-family:Courier New">New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;1st&quot;}</span>);<br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;2nd&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;3rd&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;4th&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;5th&quot;});</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;6th&quot;});</span><br/>context.SaveChanges();</span><br/><br/>ID column of Person entity is Auto Increment in database.<br/><strong>I would expect to have this result</strong> (taken for granted that table is brand new)...<br/><span style="font-family:Courier New">ID | Name<br/>==========<br/>1  | 1st<br/>2  | 2nd<br/>3  | 3rd<br/>4  | 4th<br/>5  | 5th<br/>6  | 6th</span><br/><br/><strong>However I get this!...</strong><br/><span style="font-family:Courier New">ID | Name<br/>==========<br/>1  | 3rd<br/>2  | 2nd<br/>3<span style="font-family:Courier New">  |</span> 1st<br/>4<span style="font-family:Courier New">  |</span> 6th<br/>5<span style="font-family:Courier New">  |</span> 5th<br/>6<span style="font-family:Courier New">  |</span> 4th</span><br/><br/>So after reading this: <a href="http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d8448144-05dd-4c34-b93c-9336c3b62f6e/">http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d8448144-05dd-4c34-b93c-9336c3b62f6e/</a><br/><br/>I tried with Colin Meek's advice:<br/><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;1st&quot;}, ID=1);<br/>context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;2nd&quot;}, ID=2);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;3rd&quot;}, ID=3);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;4th&quot;}, ID=4);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;5th&quot;}, ID=5);</span><br/><span style="font-family:Courier New">context.Add(New Person { <span style="font-family:Verdana">PersonID=</span>Guid.NewGuid(), Name = &quot;6th&quot;}, ID=6);</span><br/><span style="font-family:Courier New">context.SaveChanges();<br/><br/><br/><strong><span style="font-family:Verdana">However I get this!...<br/></span></strong><span style="font-family:Courier New">ID | Name<br/>==========<br/>1  | 1st<br/>2  | 2nd<br/>3<span style="font-family:Courier New">  |</span> 6th<br/>4<span style="font-family:Courier New">  |</span> 3th<br/>5<span style="font-family:Courier New">  |</span> 5th<br/>6<span style="font-family:Courier New">  |</span> 4th</span><br/><br/>Why is not working? What is going on? Is there anyway I can get this to insert in the right order?<br/><br/>Thanks</span>Wed, 20 May 2009 18:34:23 Z2009-05-20T18:40:03Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#00df9bdd-57ca-4d0a-8831-fed203fa9d44http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#00df9bdd-57ca-4d0a-8831-fed203fa9d44Federico Silberberghttp://social.msdn.microsoft.com/Profile/en-US/?user=Federico%20SilberbergInsert order problem when there is no deterministic order, helpColin Meek said:<br/><br/><span style="font-size:x-small">When there are no foreign key constraints, server generated values or stored procedures constraining the order of operations, the EF sorts by:</span> <ul type=disc> <li style="margin:0in 0in 0pt"><span style="font-family:'Times New Roman','serif';font-size:12pt"><span style="font-family:Verdana;font-size:11px">Operation</span></span> </li> <li style="margin:0in 0in 0pt"><span style="font-family:'Times New Roman','serif';font-size:12pt"><span style="font-family:Verdana;font-size:11px">Table or stored procedure name</span></span> </li> <li style="margin:0in 0in 0pt"><span style="font-family:'Times New Roman','serif';font-size:12pt"><span style="font-family:Verdana;font-size:11px">Key values</span></span></li> </ul> In my case the Key is a uniqueidentifier which I generate by using <span style="font-family:Courier New">Guid.NewGuid(). I believe this is why is not inserting in the correct order.<br/><br/>Is there a way for objectContext to sort by the column ID which is an INT Identity? If not, what other suggestion would you have? I cannot change the structure of that table. Basically I need some kind of work around as soon as possible.<br/></span><br/>Colin, Diego, MatthieuMEZIL, Daniel, anyone?<br/><br/>Thanks<br/><br/>Thu, 21 May 2009 21:36:18 Z2009-05-21T21:36:18Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#7dcedcdc-ae9f-4a17-8ba0-1332d161c4b0http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#7dcedcdc-ae9f-4a17-8ba0-1332d161c4b0Federico Silberberghttp://social.msdn.microsoft.com/Profile/en-US/?user=Federico%20SilberbergInsert order problem when there is no deterministic order, helpCould somebody from the EF's team give me a hand on this? I really need a solution ASAP....<br/> <br/> Thanks<br/>Fri, 22 May 2009 14:15:43 Z2009-05-22T14:15:43Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#d19503ad-14ce-4576-aef8-39d0e7f2274chttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#d19503ad-14ce-4576-aef8-39d0e7f2274cFederico Silberberghttp://social.msdn.microsoft.com/Profile/en-US/?user=Federico%20SilberbergInsert order problem when there is no deterministic order, helpCan somebody from the MSFT give me an answer please???<br/> <br/> Thanks...<br/>Sun, 24 May 2009 23:41:07 Z2009-05-24T23:41:07Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#65cb5a95-ce39-43b9-b289-ba7f5d80bc60http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#65cb5a95-ce39-43b9-b289-ba7f5d80bc60Daniel Simmons - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Daniel%20Simmons%20-%20MSFTInsert order problem when there is no deterministic order, helpSorry, but there's really nothing you can do here.  The EF update mechanism follows the rules which Colin has told you, and it's the expectation of the EF's design that applications should not be sensitive to this kind of thing.  The only other thing I can imagine that you could do would be to call SaveChanges separately between each add...<br/><br/>- Danny<hr class="sig">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Wed, 27 May 2009 05:21:08 Z2009-05-27T05:21:08Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#f5bb350a-5f5e-4d7b-ae3c-b24a66f2f894http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#f5bb350a-5f5e-4d7b-ae3c-b24a66f2f894Federico Silberberghttp://social.msdn.microsoft.com/Profile/en-US/?user=Federico%20SilberbergInsert order problem when there is no deterministic order, helpDaniel,<br/><br/>Thanks a lot for getting back to me. I guess for now I will call SaveChanges each time I add a new entity....<br/><br/><br/> <blockquote>The EF update mechanism follows the rules which Colin has told you, and it's the expectation of the EF's design that applications should not be sensitive to this kind of thing.</blockquote> <br/>When you say that applications should not be sensitive to this kind of things I'm assuming you are talking about the insert order in a table like my example. If that’s the case then I have to disagree with you because data is sensitive and developers need flexibility in CRUD operations in order to achieve certain task. Basically the EF is limiting developers by forcing them to use other technologies to achieve something that it's supported by previous technologies.<br/><br/>Just to give you a quick basic example. Imagine you have a table where you need to insert a family tree so basically you need to inserts things in certain order<br/><br/>Now, has this been changed in EF 4.0? If not, would it be possible to add to EF 4.0 an exception rule so we can insert entities ordered by a column instead of using the default rules like in the example above?<br/><br/>ThanksWed, 27 May 2009 11:39:45 Z2009-05-27T12:12:36Zhttp://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#3630e208-1bb3-4eea-ac64-c3c0cc53dde6http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/bd6db5b9-ba1c-4859-8867-8fed183d5ece#3630e208-1bb3-4eea-ac64-c3c0cc53dde6Daniel Simmons - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Daniel%20Simmons%20-%20MSFTInsert order problem when there is no deterministic order, helpEven with a family tree, if you have relationships defined on the table that represent the referential integrity constraints.  The EF will automatically order things properly in order to honor those constraints.  It's very unusual that you should actually have to worry about ordering at all.<br/><br/>- Danny<hr class="sig">This posting is provided &quot;AS IS&quot; with no warranties, and confers no rights.Fri, 03 Jul 2009 04:43:23 Z2009-07-03T04:43:23Z