I'm not really sure about the differences between ef-core
with full entity framework
.
In entity framework none of your changes is actually saved in the database until you call SaveChanges
. If you assert that you are the only user of your dbContext
object, you can ask this dbContext
object for collections of the objects that will be inserted and updated when you call SaveChanges
.
As no one else is using your dbContext
object, you can be sure that between asking for these collections and the actual SaveChanges nothing changes:
using (var dbContext = new MyDbContext(...))
{
// Insert and Change some items
...
// ask the DbContext which items are Inserted / Changed
var changeTracker = dbContext.ChangeTracker;
// get all inserted items:
var allInsertedItems = changeTracker.Entries()
.Where(entry => entry.State == EntityState.Added);
// or get the original and changed value of changed Contracts:
var allChangedContracts = changeTracker.Entries<Contract>()
.Where(contact => contract.State == Enitystate.Modified)
.Select(contract => new
{
OriginalValue = contract.OriginalValue,
ChangedValue = contract.Value,
});
// when ready to save the changes:
dbContext.SaveChanges();
}