Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with modified entity records

At the moment to check for changes with my entity framework object, I check in the database and I go through each object and compare the properties with the updated, if they are updated then my date modified will be updated as well.

I find that it's getting very lengthy in code and was wondering if there's a cleaner way to check for changes using entity framework. I was thinking maybe using deepcopy with a object comparison and compare something like below. But I would have to set each of my tables to be serialized and I don't know if that's a good thing.

if (Equals(oldentity, newentity))
{
    newentity.ModifiedDate = DateTime.Now
}

My current method of tracking changes

if (oldentity.firstname != newentity.firstname || oldentity.lastname != newentity.lastname)
{
    newentity.ModifiedDate = DateTime.Now
}

The if statement is a snippet, my entity has many properties so it gets lengthy...

like image 716
Master Avatar asked Dec 21 '25 08:12

Master


1 Answers

You can give your entity an interface with CreatedDate and ModifiedDate properties:

public interface ITrackedEntity
{
    DateTime CreatedDate    { get; set; }
    DateTime ModifiedDate   { get; set; }
}

Then override SaveChanges and SaveChangesAsync in the DBContext to handle updating the properties for added and modified entities automatically:

public override int SaveChanges()
{
    this.UpdateTrackedEntities();
    return base.SaveChanges();
}

public override async Task<int> SaveChangesAsync()
{
    this.UpdateTrackedEntities();
    return await base.SaveChangesAsync();
}

private void UpdateTrackedEntities()
{
    var entities = ChangeTracker.Entries().Where(x => x.Entity is ITrackedEntity && (x.State == EntityState.Added || x.State == EntityState.Modified));

    foreach (var entity in entities)
    {
        if (entity.State == EntityState.Added)
        {
            ((ITrackedEntity)entity.Entity).CreatedDate = DateTime.UtcNow;
        }

        ((ITrackedEntity)entity.Entity).ModifiedDate = DateTime.UtcNow;
    }
}

Makes things a lot simpler.

like image 102
David Sherret Avatar answered Dec 23 '25 20:12

David Sherret