VS2017中NetStandard2.0项目Domain层(EF)添加本地数据库方案咨询
Hey there, I totally get the frustration here—service-based databases don't play nicely with .NET Standard class libraries, especially when you're working across a multi-layer setup like yours. Let's walk through two solid, free alternatives that will work seamlessly with your stack, plus explain why the service-based approach isn't working.
Why Service-Based Databases Fail Here
Service-based databases are a VS shortcut tailored for .NET Framework projects—they rely on VS's built-in tooling to auto-configure attached database files and connection strings. But .NET Standard 2.0 is a cross-platform class library framework, so it doesn't support that tight integration with VS's database attachment system. EF Core in .NET Standard needs explicit connection configuration, which the service-based approach doesn't handle properly.
Option 1: SQLite (Lightweight, File-Based, Perfect for Local Use)
SQLite is my go-to for local database scenarios—it's a single file, no server required, fully supported in .NET Standard 2.0, and completely free. Here's how to set it up:
Install NuGet Packages
- In your Domain layer (.NET Standard 2.0), install
Microsoft.EntityFrameworkCore.Sqlite - In your WPF project (.NET Framework 4.7), install both
Microsoft.EntityFrameworkCore.SqliteandMicrosoft.EntityFrameworkCore.Tools(this lets you run EF migration commands)
- In your Domain layer (.NET Standard 2.0), install
Configure Your DbContext
Update your EF DbContext in the Domain layer to use SQLite:using Microsoft.EntityFrameworkCore; using YourProject.Model; namespace YourProject.Domain { public class AppDbContext : DbContext { // Replace with your actual model sets public DbSet<Customer> Customers { get; set; } public DbSet<Order> Orders { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // The database file will be created in your WPF project's output folder // (since WPF is your startup project) optionsBuilder.UseSqlite("Data Source=AppLocalDb.db"); } } }Generate the Database
- Open the Package Manager Console in VS
- Set the Startup Project to your WPF project, and the Default Project to your Domain layer
- Run
Add-Migration InitialCreateto create your first migration - Run
Update-Databaseto generate the SQLite database file
Option 2: SQL Server LocalDB (Familiar SQL Server Ecosystem)
If you prefer sticking with SQL Server, LocalDB is a free, lightweight version that comes pre-installed with VS2017. It works great with .NET Standard 2.0 and EF Core:
Install NuGet Packages
- In your Domain layer, install
Microsoft.EntityFrameworkCore.SqlServer - In your WPF project, install
Microsoft.EntityFrameworkCore.SqlServerandMicrosoft.EntityFrameworkCore.Tools
- In your Domain layer, install
Configure Your DbContext
Update your DbContext to use LocalDB:protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // LocalDB connection string—this will create a database in LocalDB's default location optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=YourAppLocalDb;Trusted_Connection=True;"); }Generate the Database
Follow the same migration steps as Option 1: runAdd-Migration InitialCreatethenUpdate-Databasefrom the Package Manager Console (with WPF as startup project, Domain as default project).
Both of these options will work smoothly with your multi-layer setup—SQLite is great for super lightweight, portable deployments, while LocalDB is perfect if you need SQL Server-specific features.
内容的提问来源于stack exchange,提问作者jolynice

