简体   繁体   English

在内存中使用 EntityFrameWorkCore 和 SqlLite 我得到“没有这样的表:ControlGroup”错误

[英]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.

相关问题 如何解决SQLLite“没有这样的表”错误? - How to resolve an SQLLite “no such table” error? EntityFrameworkCore.SQLLite和UWP行为异常 - EntityFrameworkCore.SQLLite and UWP Not behaving as expected 使用C#的SQLlite中的.ExecuteNonQuerry()错误 - .ExecuteNonQuerry() error in SQLlite using C# blazor 与 MySql EntityFrameworkCore 返回 output 参数获取错误 - blazor with MySql EntityFrameworkCore return output parameter get error 使用时为什么会出现内存不足错误? - Why do I get the an out of memory error when I use using? EntityFrameworkCore SQLite内存中的数据库表未创建 - EntityFrameworkCore SQLite in-memory db tables are not created 使用串行端口时,出现错误:尝试读取或写入受保护的内存 - Using serial ports, I get an error: Attempted to read or write protected memory EntityFrameworkCore在查询中使用错误的列 - EntityFrameworkCore using wrong column in query 使用 EntityFrameworkCore 搭建脚手架后,如何从数据库中获取行数 - How do i get the row Count from my Database after i've scaffolded it with EntityFrameworkCore 使用ASP.NET Core从MS EntityFrameworkCore中的Json文件获取记录 - Get records from Json file in MS EntityFrameworkCore using asp.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM