gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:39:43 26 4
gpt4 key购买 nike

我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com