66Gramms
66Gramms

Reputation: 743

Entity Framework Core detect changes to the Database

I'd like to call a function every time the database has any kind of changes. How can I see if the database was updated and call a function at that same moment?

Upvotes: 1

Views: 2004

Answers (1)

Farhad Zamani
Farhad Zamani

Reputation: 5861

If you are using the EntityFramework Core you can override the SaveChanges() method in DbContext file and call your function like this.

public override int SaveChanges()
{
    // call your function before changes 
    return base.SaveChanges();
    // call your function after changes 
}

And when you add any change in DataBase and call SaveChanges() method your functions run automatically.

You can get all changes in EF like this

public override int SaveChanges()
{
    var changedEntities = ChangeTracker.Entries().ToList();
    // call your function before changes 
    return base.SaveChanges();
    // call your function after changes 
}

Upvotes: 1

Related Questions