开发树莓派3上的UWP程序:EF与SQLite适配包选择咨询
Hey there! Congrats on getting your Hello World UWP up and running on your Raspberry Pi 3 with Windows 10 IoT Core—you’re already halfway there. Let’s cut through the NuGet confusion and get you set up with SQLite + Entity Framework Core properly.
Forget the random old packages you might find—stick to these official, maintained ones that play nicely with UWP and IoT Core:
Microsoft.EntityFrameworkCore.Sqlite: This is the official EF Core provider for SQLite, and it includes all the native SQLite dependencies tailored for UWP. Pick a stable version (like 6.x or 7.x) that matches your project’s target .NET framework (IoT Core supports .NET Core 3.1 and newer, so align accordingly).Microsoft.EntityFrameworkCore.Tools: Install this alongside the SQLite package (same version!)—it gives you access to EF Core commands like migrations and scaffolding, which are essential for managing your database schema.
You don’t need any extra SQLite-specific packages (like SQLite.Net-PCL)—the above two cover everything, and mixing old packages will just cause conflicts.
Since you’re targeting Raspberry Pi’s IoT Core, keep these critical points in mind:
- Target Platform Version: Make sure your UWP project targets a Windows 10 version that IoT Core supports (minimum 17763, but latest stable is always better for compatibility).
- Database File Location: UWP apps on IoT Core run in a sandbox, so never use hardcoded absolute paths. Instead, use the app’s local storage folder:
var dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "MyAppDatabase.db"); - Migration Strategy: Raspberry Pi has limited resources, so avoid running migrations directly on the device. Instead, generate migration scripts on your development machine, apply them locally, then either package the pre-built database with your app or run
db.Database.Migrate()once on app startup (only for small schema changes).
Here’s a minimal working setup to get you started:
1. Define Your Entity
public class SensorReading { public int Id { get; set; } public DateTime RecordedAt { get; set; } public double Temperature { get; set; } }
2. Create Your DbContext
using Microsoft.EntityFrameworkCore; using Windows.Storage; using System.IO; public class AppDbContext : DbContext { public DbSet<SensorReading> SensorReadings { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "SensorDb.db"); optionsBuilder.UseSqlite($"Data Source={dbPath}"); } }
3. Initialize the Database
Add this code to your app’s startup (like in App.xaml.cs):
using (var db = new AppDbContext()) { // Creates the database if it doesn't exist (great for simple apps) // For migrations, use db.Database.Migrate() instead db.Database.EnsureCreated(); }
- Mismatched Package Versions: All EF Core-related packages must be the same version—mixing 6.x and 7.x will throw runtime errors.
- Using Old EF (Not Core): UWP on IoT Core only supports Entity Framework Core—forget about the classic Entity Framework, it won’t work.
- Debugging Directly on Pi: Always test your database logic in the UWP simulator first. Debugging on the Raspberry Pi is slower, so catch issues locally first.
内容的提问来源于stack exchange,提问作者Paul Sinnema

