带有EF Core的沙盒Sqlite [英] Sandbox Sqlite with EF Core

查看:73
本文介绍了带有EF Core的沙盒Sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小型Web应用程序,当前正在使用 sqlite + EF Core 2.1 来存储数据.有什么我可以沙盒化我的sqlite数据库文件的解决方案吗?

I'm making a small web app which currently is using sqlite + EF Core 2.1 for storing data. Are there any solutions that I can sandbox my sqlite database file ?

我的意思是将我的 sqlite 数据库及其数据触发器从我的物理 db 文件加载到内存中.

I mean to load my sqlite database with its data and triggers from my physical db file into memory.

每次打开/关闭VS调试器时,数据库都会使用物理文件中的现有数据刷新.

Everytime when I turn on/off VS debugger, database will be refreshed with my existing data in physical file.

谢谢

推荐答案

以下是一些代码,它们确实可以满足您的要求:

Here's some code that literally does what you asked:

var sandboxConnection = new SqliteConnection("Data Source=:memory:");
sandboxConnection.Open();

using (var physicalConnection = new SqliteConnection("Filename=physical.db"))
{
    physicalConnection.Open();
    physicalConnection.BackupDatabase(sandboxConnection);
}

optionsBuilder.UseSqlite(sandboxConnection);

这篇关于带有EF Core的沙盒Sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆