我正在使用内存数据库提供程序进行集成测试,但是我似乎无法更新记录。我已经针对真实的 SQL 数据库运行了相同的代码,并且一切都得到了很好的更新。这是我的测试装置代码。
测试夹具:
public class TestFixture<TStartup> : IDisposable
{
private readonly TestServer _testServer;
public HttpClient TestClient { get; }
public IDatabaseService DbContext { get { return _testServer.Host.Services.GetService<DatabaseService>(); } }
public TestFixture() : this(Path.Combine("src")) { }
protected TestFixture(string relativeTargetProjectPatentDir)
{
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Testing");
var builder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddDbContext<DatabaseService>(options =>
options.UseInMemoryDatabase("TestDB")
.EnableSensitiveDataLogging());
})
.UseEnvironment("Testing")
.UseStartup<Startup>();
_testServer = new TestServer(builder)
{
BaseAddress = new Uri("http://localhost:5010")
};
TestClient = _testServer.CreateClient();
TestClient.BaseAddress = _testServer.BaseAddress;
}
public void Dispose()
{
TestClient.Dispose();
_testServer.Dispose();
}
}
我花了一天的大部分时间在谷歌上搜索这个问题,但没有遇到任何其他人谈论它,所以我假设这可能是我的问题而不是 EF bug。我确信有人会注意到您无法更新的数据库。
更新适用于 Singleton,但我有 CQRS 架构,为了检查条目是否在 e2e 测试中更新,我必须重新加载条目
Context.Entry(entity).Reload();
我希望这可以帮助别人
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)