<?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>Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx</link><description>This is the 19th post in my ongoing series of Entity Framework Tips . 
 Background: 
 If you have a table with a timestamp column, and you reverse engineer an Entity from that table, you will end up with a Binary property in your entity (in my example</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#10280504</link><pubDate>Fri, 09 Mar 2012 17:29:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10280504</guid><dc:creator>Alex D James</dc:creator><description>&lt;p&gt;Paul... yeah good question.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m often guilty of that particular sin - thanks for pointing it out!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10280504" width="1" height="1"&gt;</description></item><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#10273984</link><pubDate>Tue, 28 Feb 2012 18:27:05 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10273984</guid><dc:creator>Paul Schirf</dc:creator><description>&lt;p&gt;Why use a class named Post in an example, when it could confuse people thinking that it has some meaning?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10273984" width="1" height="1"&gt;</description></item><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#10162433</link><pubDate>Mon, 09 May 2011 12:22:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10162433</guid><dc:creator>Michael Baarz, tecbehind.de</dc:creator><description>&lt;p&gt;Hey,&lt;/p&gt;
&lt;p&gt;until now with the actual version of entityframework it says to me that its not possible to set &amp;quot;Computed&amp;quot; for timestamp and rowversion rows. So i cant get it rid by updating the LastChanged (my ConcurrencyCheck) automatically. Whats the workaround for that? When i set LastChanged before saving i get the ConcurrencyException (normal) but when i cant set it to Computed what to do to get it rid? Help me please!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10162433" width="1" height="1"&gt;</description></item><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#10121979</link><pubDate>Sun, 30 Jan 2011 00:49:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10121979</guid><dc:creator>sweYoda</dc:creator><description>&lt;p&gt;I had a problem where I created a &amp;#39;standard&amp;#39; table with a &amp;#39;standard&amp;#39; attribute that is of type DateTime and when I created an object of that table and created a new DateTime object and added it to the right parameter of that table-object it contained milliseconds, but that was in conflict to the databasetype. So I had to create a DateTime object without miliseconds.&lt;/p&gt;
&lt;p&gt;//This code failed:&lt;/p&gt;
&lt;p&gt;Order order = new Order();&lt;/p&gt;
&lt;p&gt;DateTime dt = DateTime.Now;&lt;/p&gt;
&lt;p&gt;order.timeOrdered = dt;&lt;/p&gt;
&lt;p&gt;db.AddToOrders(order);&lt;/p&gt;
&lt;p&gt;db.SaveChanges(); // OptimisticConcurrencyException&lt;/p&gt;
&lt;p&gt;//This worked:&lt;/p&gt;
&lt;p&gt;Order order = new Order();&lt;/p&gt;
&lt;p&gt;DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);&lt;/p&gt;
&lt;p&gt;order.timeOrdered = dt;&lt;/p&gt;
&lt;p&gt;db.AddToOrder(order);&lt;/p&gt;
&lt;p&gt;db.SaveChanges();&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10121979" width="1" height="1"&gt;</description></item><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#10026748</link><pubDate>Thu, 17 Jun 2010 22:13:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10026748</guid><dc:creator>Hugh</dc:creator><description>&lt;p&gt;Please help, I have a real problem with Concurrency in EF4.&lt;/p&gt;
&lt;p&gt;I have 3 database tables: User, Session, and Workstation.&lt;/p&gt;
&lt;p&gt;I am using Concurrency mode = Fixed and StoreGeneratedPattern=Computed on timestamp columns in Workstation and User, but not Session.&lt;/p&gt;
&lt;p&gt;I create a new Session object. &amp;nbsp;It has relationships to User and Workstation objects. &amp;nbsp;I just set those relationship navigation properties directly to existing objects I already had selected from the Context before.&lt;/p&gt;
&lt;p&gt;Next I save the Session object by calling Context.SaveChanges(). &amp;nbsp;When I use SQL Profiler to look at the SQL, it always updates all 3 objects. &amp;nbsp;But I only want it to save the Session. &amp;nbsp;It is re-saving the Workstation and the User objects (just touching the Timestamp and selecting it back) because they have the Fixed Concurrency Timestamp fields and they participate in a relationship with the new Session.&lt;/p&gt;
&lt;p&gt;This is a performance concern when using EF4, since I am only changing one object I just want to save one object to the database. &amp;nbsp;How can I prevent EF4 from also saving the related objects when I have Concurrency mode=Fixed and StoreGeneratedPattern=Computed on timestamp columns in those related tables?&lt;/p&gt;
&lt;p&gt;Thanks for your help,&lt;/p&gt;
&lt;p&gt;Hugh&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10026748" width="1" height="1"&gt;</description></item><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#9905240</link><pubDate>Fri, 09 Oct 2009 05:54:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9905240</guid><dc:creator>Alex D James</dc:creator><description>&lt;p&gt;@Renato and Iv&amp;#225;n&lt;/p&gt;
&lt;p&gt;Hmm I'm not sure what the limitation is with Model First, but what you need in the SSDL is a timestamp column for SQL server.&lt;/p&gt;
&lt;p&gt;Hope this helps&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9905240" width="1" height="1"&gt;</description></item><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#9905022</link><pubDate>Thu, 08 Oct 2009 19:25:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9905022</guid><dc:creator>Iván</dc:creator><description>&lt;p&gt;Tried it but as soon as I want to 'generate database script from model', I get the 'The given key was not present in the dictionary' error.&lt;/p&gt;
&lt;p&gt;Would you please tell us what the equivalent settings in the database column version is for:&lt;/p&gt;
&lt;p&gt;'ConcurrencyMode = Fixed'&lt;/p&gt;
&lt;p&gt;'StoreGeneratedPattern = Computed'&lt;/p&gt;
&lt;p&gt;That way I can try to create the table first and them update the model.&lt;/p&gt;
&lt;p&gt;I have the same problem. I need help, please&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9905022" width="1" height="1"&gt;</description></item><item><title>re: Tip 19 – How to use Optimistic Concurrency with the Entity Framework</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#9711446</link><pubDate>Tue, 09 Jun 2009 04:45:06 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9711446</guid><dc:creator>Renato</dc:creator><description>&lt;p&gt;Tried it but as soon as I want to 'generate database script from model', I get the 'The given key was not present in the dictionary' error.&lt;/p&gt;
&lt;p&gt;Would you please tell us what the equivalent settings in the database column version is for:&lt;/p&gt;
&lt;p&gt;'ConcurrencyMode = Fixed'&lt;/p&gt;
&lt;p&gt;'StoreGeneratedPattern = Computed'&lt;/p&gt;
&lt;p&gt;That way I can try to create the table first and them update the model.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9711446" width="1" height="1"&gt;</description></item><item><title>Meta-Me : Entity Framework Tips</title><link>http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx#9709065</link><pubDate>Mon, 08 Jun 2009 21:44:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9709065</guid><dc:creator>Meta-Me : Entity Framework Tips</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx"&gt;http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx&lt;/a&gt;&lt;/p&gt;
&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9709065" width="1" height="1"&gt;</description></item></channel></rss>