简体   繁体   English

使用 EF Core 加载和持久化内存中 SQLite 数据库

[英]Loading and persisting an In-Memory SQLite database with EF Core

I'm doing some research for a new project and I'm trying to determine if it's possible (and advisable) to load a SQLite database into memory, perform CRUD operations against it, and persist it back out.我正在为一个新项目做一些研究,我正在尝试确定是否可以(并且建议)将 SQLite 数据库加载到 memory 中,对其执行 CRUD 操作,并将其持久化。

I've seen many examples of utilizing SQLite databases (in memory) for unit testing, and in all of those examples, the data is just trashed in the end - this is NOT what I wish to do.我已经看到很多使用 SQLite 数据库(在内存中)进行单元测试的例子,在所有这些例子中,数据最终都被丢弃了——这不是我想要做的。

I'm going to likely use Microsoft SQL Server to manage the overall site data and act as a storage engine (users and credentials and their associated SQLite databases, etc).我可能会使用 Microsoft SQL 服务器来管理整个站点数据并充当存储引擎(用户和凭据及其关联的 SQLite 数据库等)。

When a user selects a SQLite database in the UI, I would like to load it into memory on the server, allow the user to operate against it, and then persist it back to the storage engine (SQL Server) without needing to save a.db file to the filesystem.当用户在 UI 中选择一个 SQLite 数据库时,我想将其加载到服务器上的 memory 中,允许用户对其进行操作,然后将其持久化回存储引擎(SQL Server)而无需保存。 db 文件到文件系统。

I'm comfortable with aspects of EF Core + SQL Server and SQLite (against the filesystem).我对 EF Core + SQL 服务器和 SQLite(针对文件系统)的各个方面感到满意。 But what's new to me is the idea of operating against a SQLite database in memory.但对我来说新的是在 memory 中对 SQLite 数据库进行操作的想法。

So my questions are:所以我的问题是:

  • Is this possible with EF Core? EF Core 可以做到这一点吗?
  • If so, how would I configure my SQLite DbContext class to accomplish this?如果是这样,我将如何配置我的 SQLite DbContext class 来完成此操作?
  • Are there any major downsides to this?这有什么主要的缺点吗?

Thank you谢谢

Is this possible with EF Core? EF Core 可以做到这一点吗?

Yes.是的。 Why not?为什么不? SQLite essentially treats in-memory database the same as any other. SQLite 本质上将内存数据库与其他数据库相同。 There are particular considerations , but you essentially insert and query data in the same way.有一些特殊的注意事项,但您基本上以相同的方式插入和查询数据。

If so, how would I configure my SQLite DbContext class to accomplish this?如果是这样,我将如何配置我的 SQLite DbContext class 来完成此操作?

Once again, refer to documentation .再次,请参阅文档 You would need to provide a specific connection string .您需要提供特定的连接字符串 (Not to discourage SO questions, but if you're going to research and test this you really should research information available on sqlite.org. It is has great, thorough documentation--at least compared to many open-source projects... sometimes a bit scattered, but still accessible.) (不要阻止这样的问题,但如果你要研究和测试这个,你真的应该研究 sqlite.org 上提供的信息。它有很好的、详尽的文档——至少与许多开源项目相比......有时有点分散,但仍然可以访问。)

Perhaps more complicated than specifying an appropriate connection string is actually loading an existing database file into memory.可能比指定适当的连接字符串更复杂的是实际上将现有数据库文件加载到 memory 中。 The default, basic behavior is to only create an empty database in memory.默认的基本行为是仅在 memory 中创建一个空数据库。 There are multiple ways to load the data, and this question has some useful answers.加载数据的方法有多种, 这个问题有一些有用的答案。

Are there any major downsides to this?这有什么主要的缺点吗?

You have apparently already identified some of the downsides, but probably no more than any project which needs to merge/synchronize databases.您显然已经发现了一些缺点,但可能不超过任何需要合并/同步数据库的项目。 There is no short answer to that question and it is much too broad for Stack Overflow.这个问题没有简短的答案,而且对于 Stack Overflow 来说太宽泛了。

You specifically mention syncing data to an SQL Server without saving the data to a disk file.您特别提到将数据同步到 SQL 服务器而不将数据保存到磁盘文件。 You will certainly have to perform a series of queries form sqlite, massage the data into a corresponding update statement for SQL Server, then execute that on the server.您肯定需要从 sqlite 执行一系列查询,将数据按摩到 SQL 服务器的相应更新语句中,然后在服务器上执行。 Perhaps there are third party tools to do that same thing for file-based databases, but I suspect that you'd end up performing the same operation with a disc file anyway.也许有第三方工具可以为基于文件的数据库做同样的事情,但我怀疑你最终会对磁盘文件执行相同的操作。

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

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