c# - 不支持 Entity Framework Core SQLite 连接字符串关键字 : version

标签 c# sqlite entity-framework-core

我使用 SQLite 数据库使用 .NET Core 2.2 创建了一个 ASP.NET MVC 网站。到目前为止它运作良好。当我想将特定于 SQLite 的关键字添加到连接字符串时,麻烦就开始了,例如

Data Source=~\\App_Data\\MyDb.db; Version=3; DateTimeFormat=UnixEpoch; DateTimeKind=Utc

现在我明白了

Keyword not supported: 'version'

我是这样注册数据库的

// ConfigureServices(IServiceCollection services)
var conn = Configuration.GetConnectionString("MyDB").Replace("~", _env.ContentRootPath);
services.AddDbContext<MyDBContext>(options => options.UseSqlite(conn));

然后MyDBContext有

public partial class MyDBContext : DbContext
{
    public MyDBContext() { }

    public SatrimonoContext(DbContextOptions<MyDBContext> options)
        : base(options) { }

    public virtual DbSet<Book> Book { get; set; }
}

然后我在我的页面模型中使用它

private SatrimonoContext _db;

public BookAccuracyListModel(SatrimonoContext dbContext)
{
    _db = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}

从那里我可以通过 LINQ 正常访问 _db。

在此处发帖之前,我对该主题进行了大量研究,我发现的最佳回复是 this

This provider is Microsoft.Data.Sqlite. Those connection strings are for System.Data.SQLite.

We support the following keywords: Cache, Data Source, Mode.

this

The issue I was having was because I was trying to create a SqlConnection instead of a SQLiteConnection. Making that change solved my issue.

但是,如果我做得“正确”,我就不会创建 SqlConnection,因此无法将其更改为 SQLiteConnection。另一个响应不包含解决方案。

那么我该如何让它以正确的方式工作呢?

最佳答案

Github中有一个线程关于这个问题。

Microsoft.Data.Sqlite 只支持三个关键字:

  • 缓存 - 私有(private)或共享
  • 数据源 - 数据库文件。可以是 URI 文件名。
  • 模式 - ReadWriteCreate、ReadWrite、ReadOnly 或 Memory。

此命名空间不支持其他关键字,但是如果您使用在 System.Data.SQLite 命名空间中提到的关键字,它将起作用,因为它们是与 System. Data.SQLite.

你的两个选择:

  1. 从连接字符串中删除 Version=3 关键字或任何其他不受支持的关键字
  2. 更改用于SQlite 连接的命名空间。

关于c# - 不支持 Entity Framework Core SQLite 连接字符串关键字 : version,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55324144/

相关文章:

c# - 将对象列表传递给 SQL Server 存储过程并使用 asp.net core 插入记录

sqlite - ELMAH 1.2 无法加载文件或程序集 'System.Data.SQLite' 或其依赖项之一

asp.net-core - 什么是 "DbContextOptions` 1"?

c# - Entity Framework 在没有任何更新的情况下创建迁移

c# - 我是否可以在不创建控制台应用程序的情况下在单独的进程中运行 C# 代码?

c# - 生成具有自定义指数大小的 2048 key ?

c# - 如何在 Visual Studio 2017 项目中启用 C# 7 的所有功能?

php - SQLite 在通过 URL 访问时显示数据

python - 如何在sqlite3 python中按行和列插入和调用

c# - 如何使用 UserManager 在 IdentityUser 上加载导航属性