A component of ASP.NET Core for creating RESTful web services that support HTTP-based communication between clients and servers.
Hi Rod,
I was wondering if this problem has been fixed. Bruce is correct that you need the .db file on both your laptop and pc for the application on both devices to work. To find the location of your current database file, you can check your appsettings.json file under ConnectionStrings, it should look like this:
// appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Data Source=your_sqlite.db"
}
If you reference the database directly in Program.cs, it should be located here:
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlite("Data Source=sqlite.db"));
Here are some options that will help you out the next time you need to your progress to carry over on multiple platforms.
Option 1: Regenerate the database on each machine
- Ensure that your
DbContextis configured correctly. - Use
dotnet ef migrations add InitialCreate(on the laptop if you haven’t already). - Commit the Migrations folder to GitHub.
- On the desktop, after cloning:
OR keep your current snippet indotnet ef database updateProgram.cs:using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService<MyDishesDbContext>(); db.Database.Migrate(); // Applies migrations and creates tables }
Make sure the .db file is not being ignored by .gitignore if you're trying to sync the file itself.
Option 2: Commit the SQLite file to GitHub
This works if your SQLite database is not sensitive or private.
- Locate the
.dbfile (e.g.,Data/Dishes.db) - Ensure it’s not in
.gitignore. - Commit it:
git add Data/Dishes.db git commit -m "Add SQLite DB" git push - On your desktop:
and the file will be available.git pull
Be aware: Git is not great for tracking binary files (like .db), so avoid frequent commits of large database files.
Option 3: Seed the database on startup
If you want a consistent starting point every time:
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<MyDishesDbContext>();
db.Database.Migrate();
if (!db.Dishes.Any())
{
db.Dishes.AddRange(
new Dish { Name = "Pho", Price = 9.99M },
new Dish { Name = "Banh Mi", Price = 4.99M }
);
db.SaveChanges();
}
}
This ensures you’ll always have data on any machine, after dotnet ef database update or Migrate().Hope this helps you out. Please let me know if you need any help.