7

Context

I have an existing project which is a data access layer that connects to a local SQL Server Express 12 database.

I now want to create a new ASP.NET Core project in the same solution which utilises this database. I also want to use the baked in user authentication with the relevant tables being added to this database.

What I've done

I've created the ASP.NET Core project successfully but it points to a newly created (localdb)MSSQLLocalDB rather than my SQL Server Express database. I changed the connection string in appsettings.json to:

"DefaultConnection": {
  "ConnectionString": "Server=MAIN-DESKTOP\\SQLEXPRESS;Database=ArbitrageSearch;Trusted_Connection=True;MultipleActiveResultSets=true"
}

I expected it to then complain about missing tables but actually the application continues to access (localdb)MSSQLLocalDB.

Question

How do I point to my existing SQL Express database and then how do I initialise the tables necessary to support user authentication?

6
  • take a look at C# Connection Strings how to configure this in App.Config file ..
    – MethodMan
    Dec 8, 2016 at 15:22
  • 1
    If the core application and express database are on the server you should simply do: .\SQLExpress it will automatically target the localhost.
    – Greg
    Dec 8, 2016 at 15:23
  • @MethodMan Asp.Net Core doesn't have an app.config it has an appsettings.json, he is correct.
    – Greg
    Dec 8, 2016 at 15:24
  • I would have him look at this Nuget Package then if my understanding is correct.. stackoverflow.com/questions/27849370/… reference the answer here
    – MethodMan
    Dec 8, 2016 at 15:27
  • Seems to work with that change and the relevant tables have appeared @Greg . Happy to mark as answered if you'll do the necessary.
    – ifinlay
    Dec 8, 2016 at 15:31

3 Answers 3

8

If the Core Application and SQL Express database are on the same server, you'll want to change the Connection String to: .\SQLExpress. This will automatically target the localhost.

Which will indicate the Express SQL Instance.

4
  "ConnectionStrings": {
    "DefaultConnection": "Server=.\\SQLExpress;Database=netCore.Project;Trusted_Connection=True;MultipleActiveResultSets=true"  
},

Note that // is required. After that update-database PM console

1

The easiest way for me to do was in Visual Studio to show SQL Server Object Explorer, possibly add the server to the list, right click on the desired database, select "Properties" copy the property "Connection String" and use that in the appsettings.json file.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.