[英]Using EntityFrameWorkCore and SqlLite in memory I get "no such table: ControlGroup" error
The context of my problem is, I want to create some unit tests to my application which executes some relational-specific code, so I can't use the standard .UseInMemoryDatabase(databaseName: dbContextName)
I changed it to .UseSqlite("DataSource=:memory:")
instead in order to fix that.我的问题的上下文是,我想为我的应用程序创建一些单元测试来执行一些特定于关系的代码,所以我不能使用标准的
.UseInMemoryDatabase(databaseName: dbContextName)
我将它更改为.UseSqlite("DataSource=:memory:")
而不是为了解决这个问题。
But now I get:但现在我得到:
SqliteException: SQLite Error 1: 'no such table: ControlGroup'.
SqliteException:SQLite 错误 1:“没有这样的表:ControlGroup”。
How can I fix this?我怎样才能解决这个问题?
You need to keep the connection open within your test scope and create the database.您需要在测试范围内保持连接打开并创建数据库。
public class Tests : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions _options;
public Tests()
{
_connection = new SqliteConnection("datasource=:memory:");
_connection.Open();
_options = new DbContextOptionsBuilder()
.UseSqlite(_connection)
.Options;
using (var context = new MyContext(_options))
context.Database.EnsureCreated();
}
public void Dispose()
{
_connection.Close();
}
[Fact]
public void Test()
{
using (var context = new MyContext(_options))
{
// use in memory database
context.ControlGroup ...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.