Extending Context.OnSavingChanges with DBContext RRS feed

  • Question

  • Hi,

    We are working on and ASP.Net MVC3 project (C# / EF4.0), and we are almost half way through. I am looking to extend the OnContextCreated function and found very good ways to do it.


        private void OnContextCreated()
          this.SavingChanges += new System.EventHandler(mySavingChanges);
        private void mySavingChanges(object sender, System.EventArgs e)
          Console.WriteLine("SavingChanges fired");

    The only issue is that it doesnt seem to work if I have DBContext code generated from the model, it works correctly without it.

    I am fairly new to EF and.Net in general, so this is a 2 part question:

    1. Is there a way to make it work with DBcontext?
    2. If no, and I am to remove the DBcontext code, will I lose any functionality or have to make any changes to my code (from a simple test Ive only found out that things like (.Add(..), .Remove(..), .Find(..), ..) will have different syntax, which i can live with.
    Saturday, August 6, 2011 8:21 AM


All replies

  • DbContext encapsulated DataContext and ObjectContext. AFAIK, i suggest you can use ObjectContext to reach this. http://msdn.microsoft.com/en-us/library/cc716714.aspx



    Just a newbie for everything.
    • Marked as answer by Larcolais Gong Wednesday, August 17, 2011 5:41 AM
    Wednesday, August 10, 2011 3:46 AM
  • Thanks, will check it out :)
    Wednesday, August 10, 2011 8:40 AM
  • Can you please explain to the rest of us what you meant. I have looked example you linked and I still don't know how to implement OnContextCreated with DbContext. Thanks


    Thursday, February 9, 2012 5:14 PM