c# - Xunit 内存数据库中新的 DBContext 类具有实时数据

标签 c# unit-testing entity-framework-core blazor in-memory-database

我正在尝试使用 EF Core 内存数据库进行单元测试,如下所示:

namespace ContosoTests
{
    public class TrendServiceTests
    {
       private static Microsoft.EntityFrameworkCore.DbContextOptions<TestContext> options = new 
       Microsoft.EntityFrameworkCore.DbContextOptionsBuilder<TestContext>()
                       .UseInMemoryDatabase(Guid.NewGuid().ToString())
                       .Options;

       private static TestContext _context = new TestContext(options);

       private readonly TrendService trendService = new TrendService(_context);

        private void SeedInMemoryDb()
        {
            if (!_context.TrendHistories.Any())
            {
                _context.TrendHistories.Add(new TrendHistory { IsActive = true, Quarter = E_Quarter.Q1, 
                TrendYear = 2020 });
            }

            if (!_context.Controls.Any())
            {
                _context.Controls.Add(new Control { IsActive = true});
                _context.Controls.Add(new Control { IsActive = true});
                _context.Controls.Add(new Control { IsActive = false});
            }

            _context.SaveChanges();
        }
}

我的 TestContext 继承自实时上下文类以避免错误:
数据库提供程序“Microsoft.EntityFrameworkCore.InMemory”、“Microsoft.EntityFrameworkCore.SqlServer”的服务已在服务提供程序中注册。服务提供者中只能注册单个数据库提供者

所以我的 TestContext 看起来像:

public class TestContext : ContosoContext
    {
        public TestContext(DbContextOptions<TestContext> options)
        {
            
        }

        public TestContext()
        {

        }
    }

当我在测试类中使用 TestContext (_context) 的新实例时,ContosoContext 的所有实时数据都在那里。 我期望它是一个没有数据的上下文类的新实例,以便我可以使用受控数据测试我的 DAL 代码。但这种情况并非如此。

我对单元测试相当陌生,因此非常感谢任何帮助。

编辑:
以下是我的 contoso 上下文类的相关部分

    public class ContosoContext: IdentityDbContext<ContosoApplicationUser>
    {
        public ContosoContext(DbContextOptions<ContosoContext> options) : base 
        (options)
        {

        }

        public ContosoContext()
        {

        }

        //all my DBSets here

        protected override void OnConfiguring(DbContextOptionsBuilder 
        optionsBuilder)
        {

            optionsBuilder.UseSqlServer("MyConnectionString");

        }
    }

所以问题是我的 onconfiguring 方法直接处理连接字符串?

最佳答案

要修复此错误,您需要删除 OnConfiguring 方法中的 optionsBuilder.UseSqlServer("MyConnectionString"); 以避免注册多个数据库提供程序,请将其更改为:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if(!optionsBuilder.IsConfigured)
        optionsBuilder.UseSqlServer("MyConnectionString");
}

关于c# - Xunit 内存数据库中新的 DBContext 类具有实时数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66518060/

相关文章:

.net - 包 'Microsoft.EntityFrameworkCore.SqlServer'与项目中的 'all'框架不兼容

c# - 在 C# 中创建一个可以采用 double 、十进制和 float 而不重复代码的方法

c# - Fluent NHibernate 异常 : An association from the table X refers to an unmapped class: System. 字符串

c# - C++ 到 C# : Pointers & Arrays

java - JMock - Expectations block 中方法的唯一子集

laravel - laravel 克隆了我的模拟对象吗?

c# - 吉他标签 API?

java - Spring单元测试用例不回滚记录插入

在 DbSet 中使用 Include 时,C# Entity Framework 返回损坏的 Json

c# - 具有数据库优先方法的 Entity Framework Core 6.0 - 使用 include 时选择中的非必要列