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.
Mick
I have the same issue!
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.
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;
ctx.ObjectContext.Attach(data);
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?
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