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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:58:32+00:00 2026-06-10T19:58:32+00:00

I’m using EF 4.3 with a Code first approach. With EF 4.3, it recommends

  • 0

I’m using EF 4.3 with a Code first approach.

With EF 4.3, it recommends using the new <entityFramework /> config section to initialize the connection string for a context.

I’ve done some searching online, and I can’t seem to find a convenient way to use this method for initializing a connection with a configurable database name.

For example, let say I have a MyDBContext entity in my application.
I want it to use the database name specified in the connection string from Initial Catalog=MyDB;

Using the old method from EF 4.1, I can do this no problem by adding the connection string to the <connectionstring> section in the config file.

<add name="MyDBContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=MyDB; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/>

If I want to use the newer configsection that EF 4.3 supports, I have no way to specify a database name in the connection string. I have tried the following but the Initial Catalog property is ignored. I believe it is ignored for good reason, because the whole thing is a DefaultConnectionFactory that could be used by multiple Contexts in my application

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="Data Source=.\SQLEXPRESS; Initial Catalog=MyDB; Integrated Security=True; MultipleActiveResultSets=True" />
        </parameters>
    </defaultConnectionFactory>
</entityFramework>

The default DB name that my codefirst approach creates as a result of using this is MyNamespace.MyDBContext

I understand that there is one way to overwrite this default naming by passing a value to the nameOrConnectionString argument on the base DBContext object.

So I could do something like this:

public MyDBContext() : base("MyDB") { }

However, if I take this approach I end up hardcoding DB names into my application and personally I do not like this.

I’m wondering is there any way I can pass a database name for my DB context conveniently from the Web.config file, or should I just continue using the <connectionstrings> section?

  • 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-10T19:58:34+00:00Added an answer on June 10, 2026 at 7:58 pm

    How often do you think the database name will have to change?

    That said, I haven’t run into this issue myself (I’m fine hard-coding the database name – it shouldn’t change) one option could be to store the database name you want in the Web.config as an application setting, and then have your code pull the value from there.

    EDIT: You may also want to take a look at http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx :

    public UnicornsContext()
        : base("name=UnicornsCEDatabase")
    {
    }
    

    With that pulling the connection string with that particular name:

    <configuration>
    <connectionStrings>
        <add name="UnicornsCEDatabase"
            providerName="System.Data.SqlServerCe.4.0"
            connectionString="Data Source=Unicorns.sdf"/>
    </connectionStrings>
    </configuration>
    

    According to http://blogs.msdn.com/b/adonet/archive/2012/01/12/ef-4-3-configuration-file-settings.aspx this should still work in 4.3.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the

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.