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

  • SEARCH
  • Home
  • 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 8210875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:13:50+00:00 2026-06-07T10:13:50+00:00

The canonical answer on where to put Database.SetInitializer calls is in Global.asax for web

  • 0

The canonical answer on where to put Database.SetInitializer calls is in Global.asax for web projects. I’m looking for another option.

We’re using Entity Framework 4.3.1 with Code First. We write web services and WinForms applications, and typically put data access code (such as DbContexts) in shared libraries.

Currently, the constructors of our DbContext descendants look like this:

public PricingContext(string connectionString)
    : base(connectionString)
{
    Database.SetInitializer<PricingContext>(null);
}

95% of the time, that’s the right default. 5% of the time (some integration tests, greenfield development, etc.) it’s not.

If we move that code into the initialization (or configuration) of our services and applications, adding a new DbContext to a library involves Shotgun Surgery. All of these projects have to be updated, even if the library doesn’t expose the context directly.

Optional arguments are one possibility:

public PricingContext(string connectionString,
    IDatabaseInitializer<PricingContext> databaseInitializer = null)
    : base(connectionString)
{
    Database.SetInitializer<PricingContext>(databaseInitializer);
}

Overriding the default strategy might involve passing the initializer through multiple layers, though.

We’ve also considered creating a reflection-based initializer that would set all contexts to a specific strategy.

What’s the best practice?

  • 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-06-07T10:13:51+00:00Added an answer on June 7, 2026 at 10:13 am

    How about creating a DBContextBootstrapper class that you can instantiate in Global.asax of every project; which in its implementation sets initializer for every context.

    This way if you add a new dbcontext you have to make change in the bootstrapper only, not in every project.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is an continuation on another StackOverflow post where no clear answer is
Their doesn't seem to be a canonical answer on stackoverflow listing the current state
I'm looking for ideas on how to restrict access to and log calls for
This is an attempt to discover a canonical answer for something that seems to
is there a canonical way to partition a table by referenced data to another
The canonical example for Twitter Bootstrap's popover feature is sort of a tooltip on
Is there a canonical way to randomize an array in Objective-C?
What's the simplest/canonical way to create an empty file in C#/.NET? The simplest way
Duplicate: What’s the canonical way to check for type in python? How do I
Is there a canonical way to test to see if the process has administrative

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.