none
Avoid updating the object if the object hasn't change RRS feed

  • Question

  • Hi 

    I am just wondering whether it is possible to tweak the entity framework not to generate any update command. 

     

    Say i have an applicant object .... if the applicant object remains same after triggering an update event i dont want to entity framework to go to database and update the record with the same data. The reason is it will generate a record in my audit table (based on a trigger), in reality nothing has been actually change. 

    I tried the following ... but this is still updating the record ... 

     

    using (var v = new v2Entities())

                {

                    var applicant = from a in v.applicants

                                    where a.id == applicantToUpdate.id

                                    select a;

                    applicant a1 = applicant.FirstOrDefault();

                    a1.applicant_name = applicantToUpdate.applicant_name; 

                    bool hasChanged = v.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified).Any();

                    if (hasChanged)

                    {

                        v.applicants.ApplyCurrentValues(a1);

                        v.SaveChanges();

                    }

                }

    ==============

     

    in the following line, 


                    a1.applicant_name = applicantToUpdate.applicant_name; 


                   /applicantoupdate.applicant name is same  as a1.applicant_name ... in reality the data hasnt change ... though i can understand the object graph may be change.... 

    how can i avoid updating the record if the object data remains same. 

     

     


    Tanvir Huda Application Architect/Consultant
    Thursday, January 12, 2012 6:15 AM

Answers

  • Of course if you created a custom object, you can put logic on the
    property setter to check if the value is the same as the existing value
    for the property of the object and bypass setting an IsDirty if values
    are the same. I doubt that an EF entity has that kind of sophistication.
     
    Thursday, January 12, 2012 9:13 AM
  • I am just investigating. I understand completely what you said. But i was hoping to find something smarter in EF model which will optimize the process. 

    Anyway if you go to code base model there is other thing too like Self Tracking Entity and creating entity proxy. 

     


    Tanvir Huda Application Architect/Consultant
    • Marked as answer by Tanvir Huda Monday, January 16, 2012 4:53 AM
    Friday, January 13, 2012 6:34 AM

All replies

  • Of course if you created a custom object, you can put logic on the
    property setter to check if the value is the same as the existing value
    for the property of the object and bypass setting an IsDirty if values
    are the same. I doubt that an EF entity has that kind of sophistication.
     
    Thursday, January 12, 2012 9:13 AM
  • I dont want to put any code. If i have to write code, i can do a lot more. But my question is there any settings available with in ENTITY FRAMEWORK which will allow to check the objects. Or do object comparison before update. I am after out of the box facility ...

    I doubt this feature is available in EF.

     

     


    Tanvir Huda Application Architect/Consultant
    Thursday, January 12, 2012 9:22 AM
  • On 1/12/2012 4:22 AM, Tanvir Huda wrote:
    > I dont want to put any code. If i have to write code, i can do a lot
    > more. But my question is there any settings available with in ENTITY
    > FRAMEWORK which will allow to check the objects. Or do object comparison
    > before update. I am after out of the box facility ...
     
    I am telling you no. I said a custom object you can do this with and not
    EF if you are using the Model First approach. It doesn't care if it's
    the same data or not. You address the setter on the object's property,
    then it's raising the event that it has been modified.
    >
    > I doubt this feature is available in EF.
     
    Now, I suspect that you can do it using the Code First approach, because
    you can create those objects by hand and put such logic on the setter I
    suspect.
     
    • Marked as answer by Tanvir Huda Monday, January 16, 2012 4:52 AM
    • Unmarked as answer by Tanvir Huda Monday, January 16, 2012 4:52 AM
    Thursday, January 12, 2012 9:32 AM
  • I agree with you if i write custom object then it is possible. But you missed my point in my previous post " I dont want to put any code. If i have to write code, i can do a lot  more. " ... 

    So I guess the only way is doing that is writing code .... Entity Framework cant check this by itself. 



    Tanvir Huda Application Architect/Consultant
    Friday, January 13, 2012 12:48 AM
  • On 1/12/2012 7:48 PM, Tanvir Huda wrote:
    > I agree with you if i write custom object then it is possible. But you
    > missed my point in my previous post "I dont want to put any code. If i
    > have to write code, i can do a lot more." ...
     
    What part of it that you don't understand here? I have told you twice
    that there is no feature in an EF entity for what you a trying to
    achieve. The object is either dirty or it's not dirty based on you
    setting the object's set property. There is no functionality in a
    generated entity using the database first approach where you can do what
    you are looking to do. That's it. What else needs to be explained here.
    >
    > So I guess the only way is doing that is writing code .... Entity
    > Framework cant check this by itself.
     
    Use the code first approach with EF and see if you can come up with
    something, if the functionality is that important to you.
     
    Friday, January 13, 2012 6:04 AM
  • I am just investigating. I understand completely what you said. But i was hoping to find something smarter in EF model which will optimize the process. 

    Anyway if you go to code base model there is other thing too like Self Tracking Entity and creating entity proxy. 

     


    Tanvir Huda Application Architect/Consultant
    • Marked as answer by Tanvir Huda Monday, January 16, 2012 4:53 AM
    Friday, January 13, 2012 6:34 AM
  • Hi Tanvir Huda,

    Welcome to MSDN Forum.

    I agree with darnold924. Entity Framework has no feature to judge whether the entity's value has changed. You can implement the feature by code. Entity Framework is still on the way, I think it will have more useable features in the future. If you need the feature, please suggest here. I think with your help, the Entity Framework will be more perfect.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 16, 2012 2:22 AM
    Moderator
  • Thanks Allen 

    Posted a suggestion .... http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/2517099-entity-framework-sql-update

    Cheers 

     


    Tanvir Huda Application Architect/Consultant
    Monday, January 16, 2012 4:57 AM