locked
Entity Framework create update query for unchanged data. RRS feed

  • Question

  • I am trying to update record with LINQ but in some case value is same as original value then also Enitty framework create Update query unnecessary.

    var objForupdate = context.temp.FirstOrDefault();
    if(objForupdate != null)
    {   
    // Original Value also   objForupdate.Name = "";   
    }
    // Create update query for above.
    context.SaveChanges(); 
    Here I have scenario where my DB has 20 fields. Sometime some data is same as original data then also Entity framework create update query for that.

    • Moved by CoolDadTx Tuesday, October 8, 2013 5:16 PM EF related
    Tuesday, October 8, 2013 2:38 PM

Answers

  • <copied>

    Yes I can understand it.

    But I use profile for MySQL to check what kind of query perform after save changes and I found update query. Where its old and new both values are same so, it is not meaningful. Because of this we are fighting with performance.

    <end>

    You can check the ContextState to see if there is a entity in a Modified state before executing a SaveChanges().

    Also SaveChanges() is overidable.

    http://msdn.microsoft.com/en-us/library/vstudio/bb738471(v=vs.100).aspx

    • Marked as answer by VJ Dalvi Wednesday, October 9, 2013 9:47 AM
    Wednesday, October 9, 2013 6:46 AM

All replies

  • If the EF object never changed by you setting a property on the object to set the Entity is Dirty Flag, then the SaveChanges does nothing. It does not save the object nor does it update the database.

    Tuesday, October 8, 2013 3:11 PM
  • Hi ;

    Like darnold924 stated in his post only those records that have fields that have changes will be updated to the database. So if you have a record that has 20 fields and only one field was changed then an update is sent to the database with all 20 fields in the update.

       


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, October 8, 2013 7:30 PM
  • Yes I can understand it.

    But I use profile for MySQL to check what kind of query perform after save changes and I found update query. Where its old and new both values are same so, it is not meaningful. Because of this we are fighting with performance.

    Wednesday, October 9, 2013 4:44 AM
  • Hi VJ;

    Is it possible that one of the 20 fields in the record is different?

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, October 9, 2013 4:48 AM
  • <copied>

    Yes I can understand it.

    But I use profile for MySQL to check what kind of query perform after save changes and I found update query. Where its old and new both values are same so, it is not meaningful. Because of this we are fighting with performance.

    <end>

    You can check the ContextState to see if there is a entity in a Modified state before executing a SaveChanges().

    Also SaveChanges() is overidable.

    http://msdn.microsoft.com/en-us/library/vstudio/bb738471(v=vs.100).aspx

    • Marked as answer by VJ Dalvi Wednesday, October 9, 2013 9:47 AM
    Wednesday, October 9, 2013 6:46 AM
  • Thank you darnold.

    I do the same thing but I am not override it. I use following function to change state. It is more flexible because; DB Table has field like whenupdateddtm then it will always call updated but, with following function you can check if fields get modify then set whenupdateddtm field with DateTime.Now.

            /// <summary>
            /// Changes Entity Object state if it's value not modified.
            /// </summary>
            /// <param name="entity">The entity.</param>
            /// <param name="context">The context.</param>
            /// <returns>Return IsStateChange to EntityState.Unchanged If yes then return true else return false.</returns>
            public static bool ChangeStateIfNotModified(this EntityObject entity, ObjectContext context)
            {
                if (entity.EntityState == EntityState.Modified)
                {
                    ObjectStateEntry state = context.ObjectStateManager.GetObjectStateEntry(entity);
                    DbDataRecord orig = state.OriginalValues;
                    CurrentValueRecord curr = state.CurrentValues;
     
                    bool changed = false;
                    for (int i = 0; i < orig.FieldCount; ++i)
                    {
                        object origValue = orig.GetValue(i);
                        object curValue = curr.GetValue(i);
                        if (!origValue.Equals(curValue) && (!(origValue is byte[]) || !((byte[])origValue).SequenceEqual((byte[])curValue)))
                        {
                            changed = true;
                            break;
                        }
                    }
     
                    if (!changed)
                    {
                        state.ChangeState(EntityState.Unchanged);
                    }
                    return !changed;
                }
                return false;
            }


    • Edited by VJ Dalvi Thursday, October 10, 2013 4:56 AM
    Wednesday, October 9, 2013 9:51 AM