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 8988755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:05:00+00:00 2026-06-15T22:05:00+00:00

i use EF5 code-first and set my initializers in the config. I want to

  • 0

i use EF5 code-first and set my initializers in the config. I want to use more than one database of the same context-type in one application. My connection strings look as follows:

<add name="DatabaseProduction" connectionString="Server=localhost; Database=DatabaseProduction; User Id=***; password=***" providerName="System.Data.SqlClient"/>
<add name="DatabaseTest" connectionString="Server=localhost; Database=DatabaseTest; User Id=***; password=***" providerName="System.Data.SqlClient"/>

I can use my contexts in the code like this

var _contextProd = new MyContext("DatabaseProduction");
var _contextTest = new MyContext("DatabaseTest");

The normal way to set the database initializer is the config file as explained here. But as far as i understand i can set the initializer on a per-context basis but not one for each connection string. Wrong?

The question is: How to set e.g. a DropCreateAllwaysInitializer for the Test Database while having a CreateDatabaseIfNotExistsInitializer on the Production Database?

============ Edit in answer to Gert’s comment and for clearification ============

The way i usually define my Initializers is

<contexts>
  <context type=" Namespace.MyContext, MyAssembly">
    <databaseInitializer type="Namespace.MyCustomContextInitializer, MyAssembly" />
  </context>
</contexts>

And I could for sure add a second <context...> here and define the initializer for another context. But how to tell which initializer is used at which connection string? My problem in other words:

  1. The connection string has no knowledge about which context is there.
  2. the context (in the context section) has no knowledge at which connection string it is linked
  3. In code behind i tell the context which connection string to use
  4. the initializer for a particular context seems to be kind of static for each context type.

Again, how do i define a particular initializer for a pair of context and connection string?

  • 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-15T22:05:01+00:00Added an answer on June 15, 2026 at 10:05 pm

    The only way I can think of is to derive a context for each of your connection strings:

    public class MyProductionContext : MyContext
    {
      public MyProductionContext()
        : base( "DatabaseProduction" )
      {
      }
    }
    

    and

    public class MyTestContext : MyContext
    {
      public MyTestContext()
        : base( "DatabaseTest" )
      {
      }
    }
    

    You can then set your initializers for each of the derived contexts.

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

Sidebar

Related Questions

I am programming with EF5-code first and want to use LocalDb. How can I
I'm trying to use the awsome EF5 with code first - where I need
i use the EF5/VS2012 for my application. My old DB (Database first approach) has
I use EF 5 and Code First. Ive got a main ViewModel that exposes
I have been trying to use code-first migrations, and had some limited success, but
I'm using Code-First DBContext-based EF5 setup. In DbMigrationsConfiguration.Seed I'm trying to fill DB with
USE master GO DECLARE @DbName nvarchar(MAX) SET @DbName = N'DataBase' ALTER DATABASE @DbName SET
I am using MySQL with dotConnector from Devart. I'm trying to use the code-first
i am trying to use EF Tracing utility with EF 5.0 (code first). but
I'm using EF 5.0 Code First, in a .NET 4 library. Trying to set

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.