简体   繁体   English

是否可以将 Sqlite 的 json 功能与 Entity Framework Core 一起使用

[英]Is it possible to use json features of Sqlite with Entity Framework Core

From what I have found on the web, Sqlite has a Json extension which support the Json operators -> and ->> but that it's not included by default.根据我在 web 上的发现,Sqlite 有一个 Json 扩展,它支持 Json 运算符->->>但默认情况下不包含它。 Is there a way to enable this extension when using Entity Framework Core?使用 Entity Framework Core 时有没有办法启用此扩展?

My situation is, I'm using Entity Framework Core with a PostgreSQL database in production but I'm using an Sqlite in memory database for unit tests.我的情况是,我在生产中使用 Entity Framework Core 和 PostgreSQL 数据库,但我在 memory 数据库中使用 Sqlite 进行单元测试。

I am executing some SQL in a repository that I'm having trouble unit testing.我在单元测试遇到问题的存储库中执行一些 SQL。 It uses PostgreSQLs Json operator ->> but when the unit tests try to run it with Sqlite, it doesn't recognise the operator.它使用 PostgreSQLs Json 运算符->>但是当单元测试尝试使用 Sqlite 运行它时,它无法识别运算符。

The line of code in question is有问题的代码行是

dbContext.Database.ExecuteSqlInterpolatedAsync($"DELETE FROM clients.client_history WHERE new_value ->> 'id' = {clientId.ToString()}");

I understand that Sqlite is not going to be a perfect match to PostgreSQL in all situations but it would be helpful if it could match the behaviour a bit more in this scenario.我知道 Sqlite 不会在所有情况下都与 PostgreSQL 完美匹配,但如果它能在这种情况下更匹配一些行为,那将会很有帮助。 Using PostgreSQL for the unit tests is not a practical solution for us due to performance / overhead in setting up the database.由于设置数据库的性能/开销,使用 PostgreSQL 进行单元测试对我们来说不是一个实用的解决方案。 Currently, this is the only part of my repository where Sqlite doesn't match the behaviour of PostgreSQL closely enough to be useful.目前,这是我的存储库中 Sqlite 与 PostgreSQL 的行为不匹配的唯一部分,以至于无法使用。

I'm using.Net6 with Entity Framework Core 6. I'm developing on Windows and deploying to Linux based Docker / Kube.netes (depending on environment).我正在使用 .Net6 和 Entity Framework Core 6。我正在 Windows 上开发并部署到基于 Docker / Kube.netes 的 Linux(取决于环境)。 We currently plan on upgrading to.Net7 / EF Core 7 at some point in the future (once it has been RTW, obviuosly) so if EF Core 7 introduces a solution to this, that would be useful to know, even if EF Core 6 can't do it.我们目前计划在未来的某个时候升级到 .Net7 / EF Core 7(一旦它成为 RTW,很明显)所以如果 EF Core 7 引入了解决方案,那将是有用的,即使 EF Core 6做不到。

SQLite supports this operator since 3.38 . SQLite 自3.38起支持此运算符。 You need to update your SQLite module used by EFC.您需要更新 EFC 使用的 SQLite 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM