ministrymason
ministrymason

Reputation: 1793

Entity Framework Update - Overwrite values or not

I am using Entity Framework 4.1 to perform CRUD operations against my database. I have turned off the following properties:

this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.AutoDetectChangesEnabled = false;

My method to update a user object

    public void Edit(User user)
    {
        _context.Entry(user).State = System.Data.EntityState.Modified;
        _context.SaveChanges();
    }

Ive retrieved:

User.Forename = Joe
User.Surname = Bloggs

Ive passed the user object to my edit method with

User.Forename = Joe
User.Surname = Bloggs

If I pass my user object to my Edit method but i haven't changed any of its properties, as above. Will the properties be over written in the database with the same value or will Entity Framework know the value hasn't changed?

Upvotes: 1

Views: 1516

Answers (1)

Jeff Ogata
Jeff Ogata

Reputation: 57833

Since you explicitly set the state to Modified, EF does send an update statement to the database even if none of the property values have changed.

If you don't want EF to update the database with the same values, you'll have to add logic to track whether the values have changed since you are setting AutoDetectChangesEnabled to false.

Upvotes: 2

Related Questions