Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7534157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:56:09+00:00 2026-05-30T05:56:09+00:00

I need to register an Event Handler for a class which is generated by

  • 0

I need to register an Event Handler for a class which is generated by a Template – the T4 Template within the EntityFramework.

Currently, we have edited the generated code to register the handler within the Constructor of the generated class (the Model Context).

Current code:

    public MyAppContext(string connectionString)
        : base(connectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        // Register the event handler
        this.Connection.StateChange += Connection_StateChange;
    }

The problem is, if the code is ever re-generated in the future, then the above code will be clobbered and the Event Handler will no longer get hooked up…

Code re-generation happens automatically from the smallest thing such as opening the EF Designer and moving a Table around on the canvas! So its imperative we DO NOT rely on leaving the custom code in the generated class.

Is there anyway we can put the registration in a partial class and leave the generated code untouched???

EG is there some sort of event that will always be fired once a constructor is called ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:56:11+00:00Added an answer on May 30, 2026 at 5:56 am

    The answer to this problem is to edit the T4 Template to put in a method call at the end of the Constructor.

    This method, in the context of partial classes generated by a template, needs to be a partial method.

    The template needs to contain a definition for the partial method.

    Then your custom partial class can implement that method and it will be called by the Constructors defined in the generated partial class – now you can regenerate that partial class as many times as you like and be guranteed that the partial method will always be called – assuming no one edits the template.

    If someone does edit the template and removes the definition of the partial method, then you will get a compiler error – easy to fix.

    If someone edits the template and removes the call to the partial method from the Constructor, then unfortunately, the compiler can’t help you – something to be aware of!

    Here is my solution in tid-bits:

    A snippet of the constructor and partial method definition in the T4 Template code ‘MyApp.Context.tt’ (see here for a great explanation of T4 syntax and its use within the EntityFramework):

    public <#=code.Escape(container)#>(string connectionString)
        : base(connectionString, ContainerName)
    {
    <#
        WriteLazyLoadingEnabled(container);
    #>
        // Call the OnContextCreated() method to perform any necessary 'post creation' setup
        OnContextCreated();
    }
    
    // Define the OnContextCreated partial method so that the accompanying partial Context 
    // class can implement this method.
    partial void OnContextCreated();
    

    The custom partial class which implements the partial method and wires up the event handler:

    public partial class MyAppContext 
    {
        /// <summary>
        /// Performs all 'post creation' operations for the MyAppContext 
        /// 
        /// *********************************
        /// NOTE: If you get a compiler error:
        /// 'No defining declaration found for implementing declaration of partial method 'OnContextCreated()'  
        /// then it is likely that the partial class MyApp.Context.cs does not contain a corresponding
        /// definition for the partial method OnContextCreated().
        /// This can occur if the MyApp.Context.tt template no longer generates the definition.
        /// SOLUTION: Edit the MyApp.Context.tt T4 template to ensure that that partial method is defined AND
        /// that it is called from EACH MyAppContext() constructor.
        /// *********************************
        /// 
        /// </summary>
        partial void OnContextCreated()
        {
            // Register the event handler
            this.Connection.StateChange += Connection_StateChange;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have decided that all my WPF pages need to register a routed event.
I need to write some code that registers an event handler for the SiteMapResolve
We need to develop guidelines for writing comments when we register code in version
It's most common practice to register routes in Application_Start event within global.asax.cs/vb file. But
I have a series of methods being called for my networking code. An event
Ok, so I have an attached property (declared in a static class) which attaches
I need to use an event system in C++. I have mainly four requirements
I need to allow other Python applications to register callback functions for events in
Do I need to register new extension types with Apple before I release an
I've got a whole directory of dll's I need to register to the GAC.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.