How do I share a Sqlite database between my desktop and laptop?

Rod Falanga 1,056 Reputation points
2025-04-19T13:27:12.5066667+00:00

I'm writing a Minimal API application using .NET 9. I started this on my laptop, then committed the changes to my GitHub repo and tried to pick up where I left off on my desktop. However, when I tried to test it using Postman to retrieve the dishes of a fictional business, I got the error:

No such table: Dishes

The tutorial I'm following uses a Sqlite database. I've created it on my laptop and expected it to work on my desktop, but it doesn't. I put this code snippet into the Program.cs file:

using (var serviceScope = app.Services.GetService<IServiceScopeFactory>()?.CreateScope())
{
    var context = serviceScope?.ServiceProvider.GetRequiredService<MyDishesDbContext>();
    context?.Database.EnsureDeleted();
    context?.Database.Migrate();
}

I think the problem is the Sqlite database that I generated using EF Core migrations on my laptop, doesn't appear to be in my desktop. How do I get that Sqlite database to be in both places? Or if that's not the issue, then what is the issue and how do I resolve it?

Developer technologies | ASP.NET Core | ASP.NET API
0 comments No comments

Answer accepted by question author

Danny Nguyen (WICLOUD CORPORATION) 7,425 Reputation points Microsoft External Staff Moderator
2025-07-28T06:46:04.99+00:00

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

  1. Ensure that your DbContext is configured correctly.
  2. Use dotnet ef migrations add InitialCreate (on the laptop if you haven’t already).
  3. Commit the Migrations folder to GitHub.
  4. On the desktop, after cloning:
       dotnet ef database update
    
    OR keep your current snippet in Program.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.

  1. Locate the .db file (e.g., Data/Dishes.db)
  2. Ensure it’s not in .gitignore.
  3. Commit it:
       git add Data/Dishes.db
       git commit -m "Add SQLite DB"
       git push
    
  4. On your desktop:
       git pull
    
    and the file will be available.

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.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,671 Reputation points
    2025-04-19T15:28:05.17+00:00

    SQLite is a local database file. Unless it’s read only, two applications can not access the same database file concurrently. If you created the db file it on your laptop, you need to copy it to the desktop. If the desktop changes the file, you would need to copy it back to the laptop to see the changes on the laptop.

    if you want the desktop and laptop to share access to a SQLite database, you should create a webapi application that hosts the database on a computer they can both access. Then the laptop and desktop could call the same webapi.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.