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
.mdfand.ldffiles inside aDatabase/folder in the project root -
I am using SQL Server with ADO.NET on it I copied the
.mdffile 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
.mdffile stored inside the project folder, and I'm trying to connect to it usingLocalDB.What I did
-
Added the
.mdfand.ldffiles inside aDatabase/folder in the project root -
I am using SQL Server with ADO.NET on it I copied the
.mdffile 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
.mdffile 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
Tuhin PaulPosted Jun 9, 2025, 2:13 AM
Your approach is valid, but there are a few things to consider for a truly portable solution:
What You're Doing Correctly
Using LocalDB (MSSQLLocalDB) which is a lightweight version of SQL Server
Storing the database files within your project structure
Using
|DataDirectory|token which points to your app's root directory1. Database File Properties
Make sure your .mdf and .ldf files have these properties in Visual Studio:
Build Action: "Content"
Copy to Output Directory: "Copy if newer"
This ensures the files are included when you build/publish the application.
2. Connection String Adjustment
Modify your connection string to be more robust:
Then in your
Program.cs, replace{AppDir}with the actual application directory:If you want even more portability (no LocalDB requirement), consider SQLite.
Prasad RaveendranPosted Jun 8, 2025, 12:43 AM
refer the below URLs
https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-ver17
https://learn.microsoft.com/en-us/ef/core/
You're doing it right if:
LocalDB is installed on the machine
DataDirectoryis set.mdfis markedPreserveNewestYou don't need multi-user access or migrations
If you want more portability, consider:
SQLite (zero setup) if relational DB is needed
EF Core + In-Memory DB for dev/test