6

I'm trying to overwrite the SaveChanges method to only save changes for a particular entity.

So I want to call DB.SaveChanges<MyEntity>();

However, I don't want to lose all the previous changes that may have occurred on the given context.

I'm working on something like below, but my objects values (current) aren't being set back to before.

What needs to change?

    public async Task<int> SaveChanges<T>() where T : class
    {
        var original = (from et in this.ChangeTracker.Entries()
                       where !typeof(T).IsAssignableFrom(et.Entity.GetType()) && et.State != EntityState.Unchanged
                       group et by new { et.State, et.CurrentValues } into grp
                       select new
                       {
                           key = grp.Key.State,
                           values = grp.Key.CurrentValues,
                           data = grp
                       }).ToList();

        foreach (var entry in this.ChangeTracker.Entries().Where(x => !typeof(T).IsAssignableFrom(x.Entity.GetType())))
        {
            entry.State = EntityState.Unchanged;
        }

        var rows = await base.SaveChangesAsync();

        foreach (var state in original)
        {
            foreach (var entry in state.data)
            {
                entry.State = state.key;
                entry.CurrentValues.SetValues(state.values);
            }
        }

        return rows;
    }
aherrick
  • 19,799
  • 33
  • 112
  • 188
  • _"What needs to change?"_ - your use of Entity Framework. It implements a Unit of Work pattern. If you want to reset entities you modified in-memory to their database values, you'll need to [reload them](https://stackoverflow.com/questions/20270599/entity-framework-refresh-context), preferably on a new context. – CodeCaster Oct 25 '17 at 15:17
  • I don't want reload them to their DB values, I want their values to be reset to before Save Changes occurred. Currently any changes I'm making to an object that is not "MyEntity" are being reset to DB values. This isn't what I want. – aherrick Oct 25 '17 at 15:20
  • 1
    You should work with view models. Just don't modify entities if you don't want to save these changes. – Gert Arnold Oct 26 '17 at 08:20
  • As @GertArnold said work with view models. Even if you manage to do what you want, you will end up with in memory data being different from database which would cause a data corruption at some point. – Smit Oct 31 '17 at 00:34
  • @Smit my problem is my view models would be essentially the same as the POCO classes I'm using in EF Core in the first place. Hate to have that duplicate code. – aherrick Oct 31 '17 at 13:34
  • Even though code would look the same, it still contains requirements of business logic. Which is some entities would be updated & saved to database, others not. Data in memory represented by Model class should eventually be same as data in database. Else it would be hard to figure out the inconsistencies especially with database generated fields. – Smit Nov 01 '17 at 14:12
  • Also, you have not written reasoning why you want to do it. Basically it is a bad design. But if you have to do it and code may not be most concise. cost-benefit tradeoff. – Smit Nov 01 '17 at 14:13

0 Answers0