Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework after save event

Is there a SaveChanges event that fires after changes are saved, but before the change tracker is updated?

I am using EF 6.

I need to perform a task whenever the status changes on a certain entity.

I have overridden SaveChanges to set this up. I can use ChangeTracker to tell what changes. When it's the correct entity with the correct change I fire of my code.

After the base SaveChanges is called, ChangeTracker no longer shows the entity as modified, so I need to do my task just before I save. However, there is a chance that SaveChanges will fail and I should not have done my task.

How can I hook into the ChangeTracker after the save, but before the model and ChangeTracker is updated?

like image 934
Don Chambers Avatar asked Dec 16 '25 13:12

Don Chambers


1 Answers

Can you override the save changes method?

//you will use a different dbcontext name than faroutEntities5
public partial class faroutEntities5 : DbContext
{
    public faroutEntities5()
        : base("name=faroutEntities5")
    {
    }

    public override int SaveChanges()
    {
        ChangeTracker.Entries()....

        return base.SaveChanges();
    }

    ...
like image 61
kblau Avatar answered Dec 19 '25 07:12

kblau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!