Entity Framework and TimeStamps and Concurrency

Entity Framework and TimeStamps and Concurrency

Old forum URL: forums.lhotka.net/forums/t/8790.aspx

Mick_G posted on Friday, April 09, 2010

I am taking the plunge into EF on my latest project. In the past I’ve adopted the method used in Rocky’s books to use a Stored Procedure, checking for a change in the TimeStamp. Since in EF handling of Stored Procedures is a real kludge in my opinion, what is everyone else doing? I have looked high and low and just haven’t found a good example.


simon replied on Friday, May 13, 2011

I have the same issue! 

ajj3085 replied on Friday, May 13, 2011

Ditch the stored procedures.  I used to have them for all operations, until I came to realize that they're not buying me anything, it was just more for me to maintain.  EF can check the timestamp column for you.  There may be a few specialized procedures worth keeping, so certainly keep those were it makes sense.

simon replied on Friday, May 13, 2011

I use the code below according to Using Csla4

 public void Child_Update()


            using (var ctx = Csla.Data.ObjectContextManager<MES.Server.Entities.MM>.GetManager(MES.Server.Entities.Database.MES))



                using (BypassPropertyChecks)



                    var data = new MES.Server.Entities.Item();

                    data.Id = Id;

                    var entityKey = new EntityKey("MM.Items", "Id", Id);

                    data.EntityKey = entityKey;



                    data.ItemNo = ItemNo;

                    data.Name = Name;

                    data.LastChanged = LastChanged;


                    var count = ctx.ObjectContext.SaveChanges();


                    if (count == 0)

                        throw new InvalidOperationException("Item.Insert");




the Lastchanged in entity  is set to Computed & Fixed.

when change the data and save, an exception throw:

Store update, insert, or delete statement affected an unexpected number of rows ({0}). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

What can i do?



ajj3085 replied on Sunday, May 15, 2011

You'll have to set the column's UpdateCheck, at least that's what its called in Linq to sql.  EF should have a similar setting.

Copyright (c) Marimer LLC