2

I'm trying to create a new asp.net core app which uses EF7 to access a local db database and I'm having some problems with the connection string. I've installed the local db service and I have already created several instances:

PS C:\Users\luisabreu> sqllocaldb info
MSSQLLocalDB
ProjectsV12
ProjectsV13
v11.0

MSSQLLocalDB is running v12:

PS C:\Users\luisabreu> sqllocaldb info mssqllocaldb
Name:               MSSQLLocalDB
Version:            12.0.2000.8
Shared name:
Owner:              GRM\luisabreu
Auto-create:        Yes
State:              Running
Last start time:    05/04/2016 08:46:17
Instance pipe name: np:\\.\pipe\LOCALDB#518FD662\tsql\query

This is the connection string I'm passing to EF:

{
  "usersCnnString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=UsersInfo;Integrated Security=True"
}

Btw, I've already tried this one too:

"usersCnnString": "Server=(localdb)\\mssqllocaldb;Database=UsersInfo;Trusted_Connection=True"

And here's the setup code I'm using for setting up ED dependencies:

services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<UsersContext>(options => options.UseSqlServer(Configuration["usersCnnString"]));

Whenever I try to access the database,I end up with the following error:

System.Data.SqlClient.SqlException (0x80131904): 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: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid.)

I'm surely doing something wrong, but what?

thanks! Luis

1
  • Is Configuration["usersCnnString"] the right thing to use here? What value does that give you? It's hard to tell without seeing your entire config file, but the property usually looks something like TopLevel:NextLevel:connection-string-name. Commented Apr 5, 2016 at 10:52

2 Answers 2

2

Well, it was something simpler...don't forget to kill iis express whenever you change the config of an ASP.NET Core app so that you'll get the correct config data when you re-run the app. The problem was that when I pasted the path:

Server=(localdb)\\mssqllocaldb...

into the json file, VS 2015 automatically escaped the string, turning it into:

Server=(localdb)\\\\mssqllocaldb...

Yes, I've noticed it, but only after trying to load the app on the browser. I corrected the path but didn't kill the IIS Express instance I was using to test my app. So, the error message was absolutely correct because it was still using the old path (which was trying to reach the mssqllocaldb instance running on a localdb server).

Again, stupid error. Hoping that my mistake will save someone else's 30 mins in the future...

Sign up to request clarification or add additional context in comments.

1 Comment

To add to this. I got here by trying to connect to (localdb)\\mssqllocaldb instead of (localdb)\mssqllocaldb in Server Explorer. Maybe somebody is helped by this.
0

if I create a project with user accounts using VS 2015 with RC1 the project template generates the connection string like this:

{
  "Server=(localdb)\\mssqllocaldb;Database=thedbnamehere;Trusted_Connection=True;MultipleActiveResultSets=true"
}

I would try making it closer to that, but also wondering if for some reason UsersInfo might not be a valid name, could be some keyword clash

I noticed that vs uses the "userSecretsId" from project.json as the dbname, not sure if that is required but might be worth trying

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.