gpt4 book ai didi

c# - 将 Entity Framework Core 3.1 与 ServiceProvider 中的 UseInMemoryDatabase 选项一起使用(范围生命周期)

转载 作者:行者123 更新时间:2023-12-04 11:17:15 25 4
gpt4 key购买 nike

我已将 Web 应用程序项目从 .NET Core 2.1 迁移到 3.1(也将 EF Core 从 2.1.1 迁移到 3.1.0)。

迁移后,一些单元测试不再工作,抛出重复键数据库异常。

我模拟了这个问题并意识到带有选项 UseInMemoryDatabase 的 EF 核心在 3.1 中表现不同,它不会清理旧数据。

在第二种测试方法中,People表已经包含从第一次测试添加的数据,这在 2.1 中没有发生

有谁知道如何使内存数据库适用于每个单元测试?

这是我的测试代码:

AppDbContext.cs

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace MyConsoleApp.Database
{
public class AppDbContext: DbContext
{
protected AppDbContext(DbContextOptions options) : base(options) { }

public AppDbContext(DbContextOptions<AppDbContext> options) : this((DbContextOptions)options)
{
}

public virtual DbSet<Person> Person { get; set; }
}

public class Person
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
}

AppUnitTest.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyConsoleApp.Database;
using System.Linq;

namespace MyConsoleAppTest
{
[TestClass]
public class AppUnitTest
{
public ServiceCollection Services { get; private set; }
public ServiceProvider ServiceProvider { get; protected set; }

[TestInitialize]
public void Initialize()
{
Services = new ServiceCollection();

Services.AddDbContext<AppDbContext>(opt => opt.UseInMemoryDatabase(databaseName: "InMemoryDb"),
ServiceLifetime.Scoped,
ServiceLifetime.Scoped);

ServiceProvider = Services.BuildServiceProvider();
}

[TestMethod]
public void TestMethod1()
{
using (var dbContext = ServiceProvider.GetService<AppDbContext>())
{
dbContext.Person.Add(new Person { Id = 0, Name = "test1" });
dbContext.SaveChanges();
Assert.IsTrue(dbContext.Person.Count() == 1);
}
}

[TestMethod]
public void TestMethod2()
{
using (var dbContext = ServiceProvider.GetService<AppDbContext>())
{
dbContext.Person.Add(new Person { Id = 0, Name = "test2" });
dbContext.SaveChanges();
Assert.IsTrue(dbContext.Person.Count() == 1);
}
}

[TestCleanup]
public virtual void Cleanup()
{
ServiceProvider.Dispose();
ServiceProvider = null;
}
}
}


MyConsoleAppTest.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MyConsoleApp\MyConsoleApp.csproj" />
</ItemGroup>

</Project>

最佳答案

您可以通过软件包控制台安装软件包

Install-Package Microsoft.EntityFrameworkCore.InMemory -Version 3.1.5
https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.InMemory

关于c# - 将 Entity Framework Core 3.1 与 ServiceProvider 中的 UseInMemoryDatabase 选项一起使用(范围生命周期),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59651007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com