For EF Core, the following context
is the DbContext
and YourModel
is the class/model to check for entity state and in this case Id
is the primary key. Replace Console.WriteLine with your actions.
List<EntityEntry> entries = context.ChangeTracker.Entries().Where(x => x.Entity is YourModel).ToList();
foreach (var entry in entries)
{
if (entry.State is EntityState.Added)
{
Console.WriteLine("Added");
}else if (entry.State is EntityState.Modified)
{
Console.WriteLine("Modified");
}else if (entry.State is EntityState.Unchanged)
{
Console.WriteLine("No changes");
}else if (entry.State is EntityState.Deleted)
{
Console.WriteLine($"Marked as deleted {((YourModel)entry.Entity).Id}");
}
}
Also, you can do the above by overriding SaveChangesAsync