Backs
Backs

Reputation: 24903

SaveChanges without AcceptChanges

Calling method SaveChanges in DbContext will call SaveChanges in ObjectContext with default save option SaveOptions.AcceptAllChangesAfterSave.

So, all changes will be accpeted after save.

I want to control it in manual mode:

  1. Detect changes
  2. Save changes
  3. Accept changes

Now, 2 and 3 are united in one operation, so I can't to do some operations between 2 and 3. How can I divide it?

Upvotes: 2

Views: 729

Answers (1)

Backs
Backs

Reputation: 24903

Based on Ivan Stoev's comment:

internal sealed class TestContext : DbContext
{
    protected ObjectContext ObjectContext => ((IObjectContextAdapter)this).ObjectContext;

    public override int SaveChanges()
    {
        //detect all changes in context
        ChangeTracker.DetectChanges();

        //write changes to database
        var result = ObjectContext.SaveChanges(System.Data.Entity.Core.Objects.SaveOptions.None);

        //do some actions with entities
        DoStuff();

        //accept all changes in entities
        ObjectContext.AcceptAllChanges();

        return result;
    }

Upvotes: 2

Related Questions