You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2017中NetStandard2.0项目Domain层(EF)添加本地数据库方案咨询

Fixing Local Database Setup in Your .NET Standard 2.0 Domain Layer (EF Core)

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:

  1. 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.Sqlite and Microsoft.EntityFrameworkCore.Tools (this lets you run EF migration commands)
  2. 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");
            }
        }
    }
    
  3. 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 InitialCreate to create your first migration
    • Run Update-Database to 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:

  1. Install NuGet Packages

    • In your Domain layer, install Microsoft.EntityFrameworkCore.SqlServer
    • In your WPF project, install Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Tools
  2. 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;");
    }
    
  3. Generate the Database
    Follow the same migration steps as Option 1: run Add-Migration InitialCreate then Update-Database from 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:19:22