Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Update check if the record has changed

I am updating a database table using EF.

Its a simple scenario in connected mode.

I get the row I want to update

var order = from o in Orders
        where o.ID = 1
        select o;

I then update the record as:

order.FirstName = "First";
order.LastName = "Last";
context.SaveChanges();

It works fine. EF checks if the field has changed and only updates the field if its a new value. I have enabled CDC on my SQL server to check that EF does not rewrite to the database if the value has not changed.

Now I want to put this check in my code for additional logic i.e. I want EF to tell me when the record was updated, and when it was not (because the value has not changed). Can anyone please tell if there is a way?

I do not want to manually check each field as I have a lot of fields to compare.

Thanks

like image 210
gunnerz Avatar asked Jul 16 '12 11:07

gunnerz


People also ask

How does Entity Framework detect changes?

Detect Changes works by detecting the differences between the current property values of the entity and the original property values that are stored in a snapshot when the entity was queried or attached. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.

How do I update an existing record in Entity Framework?

The steps to update an existing entity are quite simple. First retrieve an instance of the entity from the EntitySet<T> (in our case ObjectSet<Customer>), then edit the properties of the Entity and finally call SaveChanges() on the context.

How do I use Savechangesasync?

First you create an instance of MyEDM , add the list myList to the table MyTable , then call SaveChanges() to persist the changes to the database. It works how you want, the records get committed, but your program cannot do anything else until the commit finishes.


2 Answers

If anybody is interested, here is what I did. I created the following method to check if any field has changed before saving changes.

private Dictionary<Type, bool> IsEntityModified()
{
    Dictionary<Type, bool> entity = new Dictionary<Type, bool>();
    var items = _bentities.ObjectStateManager.GetObjectStateEntries(EntityState.Modified);
    foreach (ObjectStateEntry entry in items)
    {
        foreach (string propName in entry.GetModifiedProperties())
        {
            string oldsetterValue, newsetterValue = null;
            // Get orginal value 
            oldsetterValue = entry.OriginalValues[propName].ToString();
            // Get new value 
            newsetterValue = entry.CurrentValues[propName].ToString();

            if (oldsetterValue != newsetterValue)
            {
                entity.Add(entry.Entity.GetType(), true);
            }
        }
    }
    return entity;
}
like image 83
gunnerz Avatar answered Sep 29 '22 21:09

gunnerz


I came across this question/answer when I was looking for something similar if not the same thing. I ended up using this approach which seems to work alright for me.

var order = from o in context.Orders
        where o.ID = 1
        select o;

order.FirstName = "First";
order.LastName = "Last";

if (context.Entry(order).State != EntityState.Unchanged)
{
    // order has changed ...
}

So posting what worked for me in case it might be helpful for others who come here looking for something similar.

like image 23
ooXei1sh Avatar answered Sep 29 '22 20:09

ooXei1sh