Visual Studio 2012 Code First still uses SQLEXPRESS by default
Asked Answered
P

2

6

I created a brand new Web API project, created a simple Code First model (one class with an id and the dbcontext object, and that's it), and ran Enable-Migrations in the package manager console.

I noticed that it creates the database in SQLEXPRESS rather than LocalDB, despite the DefaultConnection string pointing to (LocalDB) in the Web.config file. This causes subsequent queries to fail, claiming that the database hasn't been initialized.

How do I get the Enable-Migrations command in VS 2012 to point to LocalDB rather than SQLExpress? I've tried installing SQL Management Studio 2012 Express and stopping the SQLEXPRESS database, but that just causes the Enable-Migration command to fail.

Any tips?

Note: I have VS 2010 installed, along with all the default software that it comes with (like SQL Server), so perhaps that's interfering.

Precaution answered 17/2, 2013 at 4:1 Comment(1)
Did you ever get an answer to this? I'm having exactly the same problem.Colossian
C
3

All I could find was this:

If SQL Express is installed (included in Visual Studio 2010) then the database is created on your local SQL Express instance (.\SQLEXPRESS). If SQL Express is not installed then Code First will try and use LocalDb ((localdb)\v11.0) - LocalDb is included with Visual Studio 2012.

Note: SQL Express will always get precedence if it is installed, even if you are using Visual Studio 2012

Here.

Colossian answered 26/2, 2013 at 20:46 Comment(0)
D
8

Looks like you have to actually specify (in the context constructor) that the DefaultConnection should be used by the context. For example:

public class QueensFinalDb : DbContext
{
    public QueensFinalDb()
        : base("name=DefaultConnection")
    {

    }
}

Otherwise I'm guessing it uses the first connection string in machine.config, which in my case is:

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
Deucalion answered 16/6, 2013 at 20:42 Comment(3)
Tom - This was exactly what I have been looking fore. I had the same issue where every time I tried to Updated-Database it would create it in SQLExpress. Implementing your change to the constructor in the context worked great... ThanksAneroid
Fantastic - SqlExpress is always used when you don't do this. I was having a ton of trouble finding where my bloody tables were!Tamtama
Another +1 had a full install of SQL 2012 for another project but this fixed the problem of it attempting to use that.Calvin
C
3

All I could find was this:

If SQL Express is installed (included in Visual Studio 2010) then the database is created on your local SQL Express instance (.\SQLEXPRESS). If SQL Express is not installed then Code First will try and use LocalDb ((localdb)\v11.0) - LocalDb is included with Visual Studio 2012.

Note: SQL Express will always get precedence if it is installed, even if you are using Visual Studio 2012

Here.

Colossian answered 26/2, 2013 at 20:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.