Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First: Where can I find the SavingChanges Event?

OK, this may be a newbie question, but how/where can I subscribe to the ObjectContext.SavingChanges event as mentioned for example in this post.

I've only two relevant classes in my demo app: The "Country" class and a class which holds the EF Code First "definitions":

internal class TestDb : DbContext
{
    public DbSet<Country> Countries { get; set; }       
}

Any hint is highly appreciated.

like image 292
Mike Avatar asked Oct 22 '11 18:10

Mike


1 Answers

You should be able to do this:

internal class TestDb : DbContext  
{  
    public void SetSavingChanges(EventHandler evt) 
    {
            var oc = this as IObjectContextAdapter;
            oc.ObjectContext.SavingChanges -= evt;
            oc.ObjectContext.SavingChanges += evt;
    }

    public DbSet<Country> Countries { get; set; }  
}  
like image 173
Erik Funkenbusch Avatar answered Oct 06 '22 01:10

Erik Funkenbusch