简体   繁体   中英

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?

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 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM