user12093268
user12093268

Reputation:

Entity Framework: Which Tables Entities are Affected by SaveChanges without Log?

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.

Upvotes: 2

Views: 814

Answers (1)

Muhammad Asad
Muhammad Asad

Reputation: 1782

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();

Upvotes: 3

Related Questions