0

I've a C# application which connects to a local DB running on SQL Server Express. The application should generate the tables.

I have the following in my appsettings.json:

"ConnectionStrings": {
    "VHDatabase": "Server=(localDb)\\SQLEXPRESS;AttachDbFilename=C:\\Program Files\\Microsoft SQL Server\\MSSQL15.SQLEXPRESS\\MSSQL\\DATA\\VHDatabase.mdf;Database=VHDatabase;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true;User Instance=true;"
  }

The connection is done with this in my startup.cs:

var connection = Configuration.GetConnectionString("VRHitDatabase");
services.AddDbContext<VHContext>
    (options => options.UseSqlServer(connection));

When I run the application no tables are created and I can't see any evidence of the connection existing. Is there any way I can do this or at least confirm my connection string is correct??

runnerpaul
  • 5,942
  • 8
  • 49
  • 118
  • 1
    The strings don't match - `VHDatabase` in appsettings vs `VRHitDatabase` in startup.cs – haldo Feb 08 '21 at 18:37
  • 1
    You should never us both Server and AttachDb in same connection string. AttachDb is meant when you do not have a server and reading uisng just a MDF file. Use one Server in connection string when MDF file is already attached to a database. Your connection string has "Integrated Security=true" which means you are using a Windows Credential. I would use SQL Server Management Studio to test your connection. Use Windows Credentials on login page and check to see if you can connect to the server.Once you login use the explorer and verify the database VHDatabase is listed as one of the databases. – jdweng Feb 08 '21 at 18:38
  • [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) – Charlieface Feb 08 '21 at 20:29

1 Answers1

1

You connect to User Instance

https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/sql-server-express-user-instances

A user instance is a separate instance of the SQL Server Express Database Engine that is generated by a parent instance. User instances allow users who are not administrators on their local computers to attach and connect to SQL Server Express databases. Each instance runs under the security context of the individual user, on a one-instance-per-user basis

If you were connected to ordinary instance you would be able to see list the connections by running sp_who stored procedure while your program is running or in a brake point after the connection is open.

How do I see active SQL Server connections?

Doncarleone512
  • 346
  • 1
  • 7