none
OnContextCreated error..... RRS feed

  • Question

  • Hi all,

    I am trying to add an OnSavingChanges method that will fire before I save. So I have a partial class and a partial method:

    public partial class crimestarEntities
    {
    	partial void OnContextCreated()
    	{ 
    		Debug.WriteLine("The context was created, yay!");
    	}
    }
    
    

    But when I try to compile, I get an error: No defining declaration found for implementing declaration of partial method 'xxxxx.OnContextCreated()' 

    If I remove the partial reference (and add public, protected, whatever), it compiles just fine, but then the method never executes. 

    According to all the links on how to apply business rules, this should work.  What am I missing?

    Thanks,

    me

     


    FletcherJ
    Tuesday, August 23, 2011 6:39 PM

Answers

  • JP,

    Ok, I thought I had the problem solved.  But when I added the partial class to the template, it did what I expected - it stopped the complier from complaining.  But it doesn't mean that the code is getting executed...

    So I added:

    partial void OnContextCreated();
    

    To the template.  Now everything compiles (as it should.)  But since there is no code that actually forces the OnContextCreated method to be executed, it still doesn't execute.  Do I need to add that to the template as well?

    If so, how can I turn off the silly template that is messing this up and start wtih a standard vanilla version again?

    Thanks,

    me

     


    FletcherJ
    • Marked as answer by FletcherJ Friday, August 26, 2011 11:57 PM
    Friday, August 26, 2011 11:53 PM

All replies

  • Partial methods are place holders, so when you declare them you need to leave that content empty, then in the other portion of the partial class "crimeStartEntitites" you would implement this method.  This is confusing but it's very powerful because if you regenerate your enitity diagram, the other partial class with the implementation of the partial method is not affected.


    JP
    Tuesday, August 23, 2011 6:44 PM
  • JP,

    Ok, I am confused (nothing new...)  I thought that I was creating the partial class to compliment what was built automatically by the EF.  MY understanding was that the EF rebuilds the main classes when the .edmx file changes and that we use the partial classes so our work doesn't get erased the next time the code is generated.

    I was under the impression that the EF defined the partial method for us to add the code as I did.

    Ok, so given that I need to define the initial partial method.  How do I get the EF to call it?

    Thanks,

    me


    FletcherJ
    Tuesday, August 23, 2011 7:26 PM
  • Hi,

    for info about how to add business logic before saving changes see:

    http://msdn.microsoft.com/en-us/library/cc716714.aspx

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, August 23, 2011 8:20 PM
    Moderator
  • From this link:  They define the OnContextCreate as follows:

     

    The Entity Data Model tools generate an OnContextCreated partial method in the class that represents the EntityContainer for the model and that inherits from the ObjectContext class. This partial method is called whenever an ObjectContext is instantiated. Implement this partial method in your code to register a handler for the SavingChanges event. For more information, see How to: Execute Business Logic When Saving Changes.

     

    This means as you've already discovered that the stub for the Partial Method is already there... (When you created the EDMX).  Now the question is how do I wire it up?  We found this code in the example from here:

    public partial class AdventureWorksEntities
    {
      partial void OnContextCreated()
      {
        // Register the handler for the SavingChanges event.
        this.SavingChanges
          += new EventHandler(context_SavingChanges);
      }
    
    

    So Base on what you posted and what the example shows the only conclusion based on the error message you mentioned is that either the class name has not been spelled correctly, or one is public one is not.  The message is indicating to you that the 'crimestarEntities' partial class cannot find the associated partial method stub.  Try this, go back to the entity class generated by the EDMX, then try to find the stub for OnContextCreated.  If you can't find it then perhaps it has something to do with the way the EDMX was generated.  If that's the case delete the model and recreate it, but pay close attention to all of the options.  I am assuming you are not using CODE FIRST, rather you are generating the model from an existing table in a database.


    JP
    Tuesday, August 23, 2011 9:56 PM
  • JP,

    Ok, I took your code, verbatum and pasted it into a new class.  I changed the name of the class and change the code to simply do an output to the output windo.  So I have the following:

    namespace crimestar.efLib
    {
    	public partial class crimestarEntities
    	{
    		partial void OnContextCreated()
    		{
    			// Register the handler for the SavingChanges event.
    			//this.SavingChanges
    			//  += new EventHandler(context_SavingChanges);
    			Debug.WriteLine("Hi from the OnContextCreated method.... ");
    		}
     
    	}
    }
    

    When I try to compile it, I get the same error....

     

     


    FletcherJ
    Friday, August 26, 2011 4:17 AM
  • Julia,

    Thanks, that (your link) was my starting point.  My problem is that I can't get the OnContextCreated partial method to work, so the rest of that code is moot. 

    It compiles fine if I use any of the following:

    Public bvoid OnContextCreated()

    Private void OnContextCreated()

    But partial void OnContextCreated() (or any of the above with partial added) results in the same error: No defining declaration found for implementing declaration of partial method 'crimestar.efLib.crimestarEntities.OnContextCreated()' 

    This leads me to believe that the EF is not defining the partial method in the first place.  Either that or I am trying to add the partial method to the wrong class.  But this is the same class that works when I create a context and use it to get data.

    I am positive that this is something really stupid, like a use of an incorrect case or something.  But even when I copy paste, I still get the same error on a build.  If I add a generic method:

    public void testMe()
    		{
    			// This will do nothing but a debug.write....
    			Debug.WriteLine("Hi from the crimestar entity context manager ....");
     
    		}

    And call that from the context manager, it does just what is expected.

    Any other ideas?

    Thanks,

    me

     

     


    FletcherJ
    Friday, August 26, 2011 4:31 AM
  • JP,

    Thanks for the link.  It's a different approach and I will certainly look into it. 

    But even if that approach works, I am still curious as to why I get the error when I do a build.  From all the code snippets and comments I have received, it should work.....

    Thanks,

    me


    FletcherJ
    Friday, August 26, 2011 4:36 AM
  • Ok do this, in your project click on Find/Find in Files, then search for all instances of OnContextCreated, you will want to find where EF 4.0 generated the content, then take that exact same signature and copy it to your partial method.  I'm just wondering one of two things 1) Either EF didn't generate the placeholder/stub or 2) The partial method not being marked Public causes it not to be found.
    JP
    Friday, August 26, 2011 11:15 AM
  • JP,

    Good idea.  I used Find in Files, but all it found was the ones in the various classes I have defined to test this.  And yes, I told it to search the entire solution.  This leads me back to the point where I believe that I have not set something that causes the EF to generate the base OnContextCreated method/placeholder/stub.....

    The partial method shouldn't need to be marked public, but I have tried it with it being marked public, private, and protected as well as with no such designator and none of them work UNLESS I remove the "partial" keyword.

    Thanks,

    me

     


    FletcherJ
    Friday, August 26, 2011 6:14 PM
  • ok good you're on right track, EF did not generate the stubs.  Did you select the boxes on the Model that ask you to allow them to be editable when you first mapped the tables from the database?  There's a column of checkboxes on right side, try that.
    JP
    Friday, August 26, 2011 6:28 PM
  • Julia,

    JP suggested that I search for OnContextCreated.  I only found the versions I defined.  So then I found the generated code that generated my entity.

    I found a class named Model1.context.cs.  In there, I found a definition for the context:

    public partial class crimestarEntities : ObjectContext
    

    So I went to the top of the page to see what methods were exposed.  I was unable to find any reference to OnContextCreated (or any of the other methods I expected to be available.) 

    I then did a search on ObjectContext and again, found only the entity and the .tt template file. 

    It appears that the EF is not creating a default OnContextCreated partial method so my attempt to define a partial one in my business rules class fails for just the reason indicated in the error message.

    Do you know how I can tell the EF to generate the EF partial method (or what I may have done that is preventing it from doing so)?  Could it be that I am using the POCO classes instead of the one large file?

    Thanks,

    me

     


    FletcherJ
    Friday, August 26, 2011 6:31 PM
  • JP,

    Ok, I thought I had the problem solved.  But when I added the partial class to the template, it did what I expected - it stopped the complier from complaining.  But it doesn't mean that the code is getting executed...

    So I added:

    partial void OnContextCreated();
    

    To the template.  Now everything compiles (as it should.)  But since there is no code that actually forces the OnContextCreated method to be executed, it still doesn't execute.  Do I need to add that to the template as well?

    If so, how can I turn off the silly template that is messing this up and start wtih a standard vanilla version again?

    Thanks,

    me

     


    FletcherJ
    • Marked as answer by FletcherJ Friday, August 26, 2011 11:57 PM
    Friday, August 26, 2011 11:53 PM