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
.mdfand.ldffiles inside aDatabase/folder in the project rootI 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 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