-2

I'm new to ASP.NET MVC and was just messing around with it and trying to see how it works. So I decided to make a query to the database accessed with the default ApplicationDbContext. Normally, if the database hadn't been instantiated before that, this query would make sure to first create the database and then make the query, right? Well, here's what I did:

// This is the default Index action in the Home controller:
public ActionResult Index()
{
     var db = new ApplicationDbContext();
     var usersCount = db.Users.Count();

     return View();
}

I know that the query is pretty meaningless but the sole purpose of it was for it to generate the database. The thing is it that it doesn't. I get a System.Data.SqlClient.SqlException when trying to execute the query (second line of the method).

Even when checking the App_Data folder I see that the database isn't created. What could be the cause of this? Strange thing is that this works on my desktop computer, but not on my laptop. But I did notice that when I put an invalid Data Source(ex. Data Source=.) in the connection string I get the same exception on my desktop computer. So maybe something is happening with the LocalDb that's causing this behaviour.

The connection string:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-DefaultProject-20160731061747.mdf;Initial Catalog=aspnet-DefaultProject-20160731061747;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

This is the full exception:

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code

Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)

I would be very grateful if someone can come up with a solution for this!

P.S: Everything works fine on my laptop if I change the Data Source to my SQL Server Express server name.

Mr. Nicky
  • 1,519
  • 3
  • 18
  • 34
  • 1
    This is the generic DB connection failed issue. Something is wrong in your connection string, or the server isn't configured to accept connections, or there's a firewall preventing it, or SQL isn't installed... etc. – TZHX Aug 01 '16 at 10:40
  • I've gone through that before. It doesn't answer my question. Every ASP.NET MVC application has an ApplicationDbContext and Models that come with it, so writing the code-first is not the problem. @MurrayFoxcroft – Mr. Nicky Aug 01 '16 at 10:41
  • I'd vote for SQL isn't installed: otherwise shouldn't the boilerplate EF startup create the db if it's not there? OP, try setting up a blank db with that name on your localhost and then running it. – jleach Aug 01 '16 at 10:42
  • @jdl134679 I have SQL Server Express installed if that's what you mean and I also think that EF should create the db if it's not there. The application works fine and connects to a database it the data source is my SQL Server Express server name. – Mr. Nicky Aug 01 '16 at 10:47
  • LocalDb is something else... it's installed with certain versions of VisualStudio and is meant to be a "dev attachment" of sorts. I bet that's what you're missing, especially seeing as how it works fine pointing the connection to your Express. (fwiw, I just point everything to my "real" instance anyway, don't particularly care for the LocalDb version) – jleach Aug 01 '16 at 10:51
  • http://stackoverflow.com/questions/23320013/how-to-install-localdb-separately – jleach Aug 01 '16 at 10:52

1 Answers1

0

Sounds like your LocalDb instance isn't installed. This is a bit of a "dev attachment" version of SQL that's installed with certain versions of Visual Studio and might have been de-selected or just plain not included perhaps if the original install was older and has gone through upgrades, etc.

This post gives a decent explaination: How to install localdb separately?

Basically google for SqlLocalDb.msi and follow the instructions.

I generally prefer Express over localDb anyway, but that's up to you.

Also note that connection strings are considered "loose" information in the web.config for MVC stuff. That is, they tend to be specific to the machine, so if you're changing machines (or working on a team), your machine is likely to have a different connection string (or a local copy/partial of web.config).

Community
  • 1
  • 1
jleach
  • 7,410
  • 3
  • 33
  • 60
  • Thanks for the extensive answer, you make a good point! I'll definitely try installing it manually! The reason why I kinda wanted to use the LocalDb is because I wanted to make use of the ApplicationDbContext, the models and all the goodies that come with the autogenerated database from ASP.NET MVC. Do you know how I can make it so that autogenerated database gets created in my SQL Server Express and not as a LocalDb? – Mr. Nicky Aug 01 '16 at 11:04
  • The target database shouldn't matter, whether it's in localdb or an express instance, all that EF/DbContext stuff should work the same, just change the connection string to point to the correct instance accordingly – jleach Aug 01 '16 at 11:05
  • So if I just change the Data Source in the connection string to my SQL Server Express server name, everything should be fine? – Mr. Nicky Aug 01 '16 at 11:07
  • that should be the case, yes – jleach Aug 01 '16 at 11:07
  • note that'll probably look like `Data Source=MyComputerName\SQLExpress2014` (e.g., two part name that has to go there, the "server" (computer name) and "instance" (whatever your sql instance is named). Alternatively you can try `localhost` as the server portion: `Data Source=localhost\SQLExpress2014` – jleach Aug 01 '16 at 11:09
  • Installing LocalDb didn't fix the problem. I also tried to changed the Data Source like you said and this time I get an exception saying that permission was denied for creating the database at the specified directory. Any suggestions how I can grant these permissions? – Mr. Nicky Aug 01 '16 at 11:25