1

My update method will always update since I have to set the LastModified date. I was wondering if there is a way to dynamically check if some values have changed.

My state objects look like this:

public partial class Action : IEntity
{
    public long Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public System.DateTime Created { get; set; }
    public System.DateTime LastModified { get; set; }
    public Nullable<System.DateTime> Deleted { get; set; }
}

The interface I use looks like this:

public interface IEntity
{
    long Id { get; set; }        
    DateTime Created { get; set; }
    DateTime LastModified { get; set; }
    DateTime? Deleted { get; set; }       
}

My update method looks like this (The changes are saved later):

    public virtual void Update(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        var attachedEntity = DbSet.Find(entity.Id);

        if (attachedEntity != null)
        {
            var attachedEntry = DbContext.Entry(attachedEntity);

            entity.Created = attachedEntity.Created;
            entity.LastModified = DateTime.Now;

            attachedEntry.CurrentValues.SetValues(entity);
        }
        else
        {
            dbEntityEntry.State = EntityState.Modified;
            entity.LastModified = DateTime.Now;
        }
    }

So it will actually perform a generic update for every object that is passed as T with the the IEntity Interface. However it performs the update every single call to this method because the LastModified value is changed. Resulting in many update queries like these:

exec sp_executesql N'update [dbo].[BiztalkEntity]
set [LastModified] = @0
where ([Id] = @1)
',N'@0 datetime2(7),@1 bigint',@0='2013-05-17 11:22:52.4183349',@1=10007

Could you tell me how to prevent the query being executed every single time?

Nick N.
  • 12,902
  • 7
  • 57
  • 75
  • [Sort of like this?](http://stackoverflow.com/a/5806708/7724) – bzlm May 17 '13 at 09:35
  • @bzlm thanks for the quick respond, but this actually works. I just want to know how to check for the changes, and then not set the value of last modified, so it won't update. – Nick N. May 17 '13 at 09:37

1 Answers1

6

I suggest you delay the setting of LastModified and let Entity Framework give you all the entities that have been changed just prior to the changes being sent to the database.

You can override the SaveChanges() method of DbContext

public class MyContext : DbContext
{
    public override int SaveChanges()
    {
        //you may need this line depending on your exact configuration
        //ChangeTracker.DetectChanges();
        foreach (DbEntityEntry o in GetChangedEntries())
        {
            IEntity entity = o.Entity as IEntity;
            entity.LastModified = DateTime.Now;
        }
        return base.SaveChanges();
    }

    private IEnumerable<DbEntityEntry> GetChangedEntries()
    {
        return new List<DbEntityEntry>(
            from e in ChangeTracker.Entries()
            where e.State != System.Data.EntityState.Unchanged
            select e);
    }
}
qujck
  • 14,388
  • 4
  • 45
  • 74
  • Sounds ok, but I don't think I can get an IEntity out of DbEntityEntry or can I? Trying it now – Nick N. May 17 '13 at 11:07
  • @NickN. DbEntityEntry.Entity http://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.entity(v=vs.103).aspx – qujck May 17 '13 at 11:09
  • @NickN. it has never given me any issue. – qujck May 17 '13 at 11:10
  • Where does the `ChangeTracker` come from? As in what namespace? – Nick N. May 17 '13 at 11:14
  • It does not affect the queries for me, do you think it's because it still detect changes? Now the `LastModified` property is still set for every single object that is saved and that's a good thing. But I just don't want some to get saved in the first place. Since they are not really changed – Nick N. May 17 '13 at 11:30
  • @NickN. it must be due to the line 'dbEntityEntry.State = EntityState.Modified' where you're basically saying "it's definitely changed". Try attaching without changing the `State`. – qujck May 17 '13 at 11:57