2

In EntityFramework Core, How can I tell which Table entities are updated after SaveChanges() is called without using Log? I want to store in variable or console writeline. Is there way to read metadata or through reflection, etc? Open to any strategy. If multiple tables are saved, would like to save in List or Enumerable,

public class CustomContext : DbContext
{
    public CustomContext ()
    {
    }

    public override int SaveChanges()
    {
        Console.Writeline("...
    }

Resource: How to make Entity Framework Data Context Readonly

Using Entity Framework Core 2.2

'Duplicate Question tracks all the original content, values, etc, I am just looking for distinct entities affected, prob need simpler code line.

  • Look up this answer, it should be helpful to you https://stackoverflow.com/a/32738370/9998747 – Jamal Salman Oct 03 '19 at 07:43
  • Possible duplicate of [Getting all changes made to an object in the Entity Framework](https://stackoverflow.com/questions/3265257/getting-all-changes-made-to-an-object-in-the-entity-framework) – Jamal Salman Oct 03 '19 at 07:44
  • hi @JamalSalman that tracks all the original content, values, etc, I am just looking for distinct entities affected, prob need simpler code line, thanks –  Oct 03 '19 at 07:46

1 Answers1

3

Try this hope this'll help you.

var dirtyEntries = context.ChangeTracker
           .Entries()
           .Where(x => x.State == EntityState.Modified || x.State == EntityState.Deleted|| x.State == EntityState.Added)
           .Select(x =>x.Entity)
           .ToList();
Muhammad Asad
  • 1,772
  • 16
  • 23