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

.NET Core 2.x跨平台读取本地SQLite文件的实现方案问询

.NET Core 2.x Cross-Platform SQLite Reading (Minimal Dependencies)

Hey there! Let's work through your .NET Core 2.x cross-platform SQLite data reading issue, keeping dependencies as lean as possible. I’ll break down why you hit that Open() error with Microsoft.EntityFrameworkCore.Sqlite.Core and share two solid, low-dependency solutions.

Why did Microsoft.EntityFrameworkCore.Sqlite.Core fail on Open()?

The Sqlite.Core package is just the EF Core SQLite core logic—it doesn’t include the underlying SQLite native runtime libraries. When you tried to call Open(), the runtime couldn’t find the platform-specific SQLite binaries, hence the error. You just need a tiny additional package to fix this.


Solution 1: Lean EF Core Approach

If you want to stick with EF Core (for its convenient ORM features), you only need two packages to get cross-platform support:

  1. Microsoft.EntityFrameworkCore.Sqlite.Core (you already have this)
  2. SQLitePCLRaw.bundle_e_sqlite3 (automatically includes platform-specific SQLite native libraries for Windows, Linux, macOS)

Code Example

using Microsoft.EntityFrameworkCore;

// Define your context and entity
public class LocalDbContext : DbContext
{
    public DbSet<YourEntity> YourData { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Point to your local SQLite file
        optionsBuilder.UseSqlite("Data Source=your-local-db.sqlite");
    }
}

public class YourEntity
{
    public int Id { get; set; }
    public string SomeValue { get; set; }
}

// Read data example
using (var context = new LocalDbContext())
{
    var records = context.YourData.ToList();
    // Process your records here
}

EF Core handles connection management automatically, so you won’t need to call Open() manually—and the native libraries are handled by the bundle package, so it works across all .NET Core 2.x supported platforms.


Solution 2: Ultra-Lightweight (No EF Core)

If you want to ditch EF entirely for minimal dependencies, use SQLitePCLRaw directly. It’s a thin wrapper around SQLite with zero extra bloat.

Required Packages

Install-Package SQLitePCLRaw.core
Install-Package SQLitePCLRaw.bundle_e_sqlite3

Code Example

using SQLitePCL;

// Initialize SQLite (call this once at app startup)
Batteries_V2.Init();

// Open the database and read data
using (var connection = SQLite3.Open("your-local-db.sqlite"))
{
    using (var statement = SQLite3.Prepare(connection, "SELECT Id, SomeValue FROM YourEntity"))
    {
        // Iterate over results
        while (SQLite3.Step(statement) == SQLite3.Result.Row)
        {
            int id = SQLite3.ColumnInt(statement, 0);
            string value = SQLite3.ColumnString(statement, 1);
            // Process each record
        }
    }
}

This is as lean as it gets—no ORM overhead, just direct SQLite operations, and the bundle package takes care of all cross-platform native components.


Why Avoid System.Data.SqlClient?

You’re right to steer clear here—System.Data.SqlClient is designed for SQL Server, not SQLite. Any SQLite support it might have relies on clunky native components that are poorly maintained for cross-platform scenarios. It’s not a viable option for SQLite in .NET Core.


Final Notes

Both solutions work seamlessly across Windows, Linux, and macOS with .NET Core 2.x, and keep dependencies to an absolute minimum. Pick the EF Core approach if you want ORM convenience, or the raw SQLitePCLRaw approach if you need maximum performance and minimal footprint.

内容的提问来源于stack exchange,提问作者S.Richmond

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:44:14