0
Answer

ASP.NET Core 9 Web API — LocalDB .mdf file — Need Help Making It Work

0

 

I'm building an ASP.NET Core 9 Web API project, and I want the whole application to be completely self-contained — no need for any external database setup, no SQL Server Management Studio (SSMS), just run the app and everything works.

I'm using a local SQL Server .mdf file stored inside the project folder, and I'm trying to connect to it using LocalDB.

What I did

  • Added the .mdf and .ldf files inside a Database/ folder in the project root

  • I am using SQL Server with ADO.NET on it I copied the .mdf file from my SQL Server (it works on SQL Server)

  • Used this connection string in appsettings.json

    "ConnectionStrings": 
    {
        "DefaultConnection": "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database\\BankAPI.mdf;Integrated Security=True;Connect Timeout=30" 
    }

    I'm building an ASP.NET Core 9 Web API project, and I want the whole application to be completely self-contained — no need for any external database setup, no SQL Server Management Studio (SSMS), just run the app and everything works.

    I'm using a local SQL Server .mdf file stored inside the project folder, and I'm trying to connect to it using LocalDB.

    What I did

  • Added the .mdf and .ldf files inside a Database/ folder in the project root

  • I am using SQL Server with ADO.NET on it I copied the .mdf file from my SQL Server (it works on SQL Server)

  • Used this connection string in appsettings.json:

    "ConnectionStrings": 
    {
        "DefaultConnection": "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database\\BankAPI.mdf;Integrated Security=True;Connect Timeout=30" 
    }
    
  • What I need help with:

  • Is this the correct way to make the .mdf file work?
  • Do I need to add or configure anything else to ensure it works on any machine without needing SSMS or SQL Server setup?
  • Any tips from someone who’s done a similar setup?
  • Does anyone have a recurse to learn and see how to do it will be very helpful

  • Am I doing things right any way?

My project folder structure looks like this:

Bank Project/
+-- API Layer/
¦   +-- Program.cs
¦   +-- appsettings.json
+-- Business Logic Layer/
+-- Data Access Layer/
+-- Database/
¦   +-- BankAPI.mdf
¦   +-- BankAPI_log.ldf