Self-contained ASP.NET Core Web API using LocalDB without separate SQL Server install
07:34 07 Jun 2025

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, 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 SQL Server setup?
  • Any tips from someone who's done a similar setup?
  • Am I doing things right any way?

Does anyone have a resource to learn and see how to do it will be very helpful

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
c# sql-server asp.net-core-webapi .net-9.0