none
How to change connection string for EF to point to a new database? RRS feed

  • Question

  • How can I point my mvc3/ef application to a new database via the web.config?  I have tried modifying the following in the web config to point to a new database (sql server express) but the application seems to keep the original db cached even after cleaning the project (of temp files)

     

    <configuration>
    
     <connectionStrings>
    
      <add name="ApplicationServices"


    Even if I comment out the connection string section the application still has the db cached somewhere and hits the original successfully.

    Any help would be greatly appreciated.  Thanks!

     

     

    Tuesday, August 23, 2011 3:45 PM

Answers

All replies

  • On 8/23/2011 11:45 AM, shayska wrote:
    > How can I point my mvc3/ef application to a new database via the
    > web.config? I have tried modifying the following in the web config to
    > point to a new database (sql server express) but the application seems
    > to keep the original db cached even after cleaning the project (of temp
    > files)
    >
    > <configuration>
    >
    >   <connectionStrings>
    >
    >    <add name="ApplicationServices"
    >
    >
    > Even if I comment out the connection string section the application
    > still has the db cached somewhere and hits the original successfully.
    >
    > Any help would be greatly appreciated. Thanks!
    >
     
    I don't think that's a connection string for SQL Server used by EF.  If
    anything, the program is finding the SQL Server connection string in
    another config file. The connection string that EF has created is in an
    app.config when you first created the EF model. And the connection
    string must be moved/copied to the root.config a Web.config or an
    app.config if the application is an exe type of application.
     
    Nothing is cached and the connection string is being found somewhere
    else other than where you think it should be found.
     
    Tuesday, August 23, 2011 4:12 PM
  • Hi,

    are you using EF 4.1?

    If the connection string name is different from the name of your context then you can tell DbContext to use this connection by passing the connection string name to the DbContext constructor.

    Take a look at the following post:

    http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

     

    thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by shayska Tuesday, August 23, 2011 9:36 PM
    Tuesday, August 23, 2011 7:37 PM
    Moderator
  • Yep, since I was using code first no overloaded constructor to pass the connection string name to the base class constructor the database that gets used is the namespace.dbcontext as the database name and the connection string is created at run-time.

     

    MyCase:

    In this example DbContext uses the namespace qualified name of your derived context class—Magic.Unicorn.UnicornsContext—as the database name and creates a connection string for this database using SQL Express on your local machine.

    namespace Magic.Unicorn
    {
      public class UnicornsContext : DbContext
      {
        public UnicornsContext()
        // C# will call base class parameterless constructor by default
        {
        }
      }
    }

     

    Explicit connection string and connection credentials that are specified in the web.config

    public class UnicornsContext : DbContext
    {
      public UnicornsContext()
        : base("UnicornsDatabase")
      {
      }
    }

    Tuesday, August 23, 2011 9:41 PM