EF Core 2.1内存DB中不更新记录 [英] EF Core 2.1 In memory DB not updating records
问题描述
我正在使用内存数据库提供程序进行集成测试,但是我似乎无法更新记录。我已经在真实的SQL数据库上运行了相同的代码,一切都得到了更新。这是我的测试装置代码。
I'm using the in memory database provider for integration tests however I don't seem to be able to update a record. I've run the same code against a real SQL database and everything gets updated fine. Here is my test fixture code.
测试装置:
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();
}
}
我一天中大部分时间都在使用Google搜索没有其他人在谈论它,所以我假设这可能是我的问题,而不是EF错误。我确定有人会注意到您无法更新数据库。
I've spent most of the day googling this and not come across any other people talking about it so I'm assuming its probably my issue rather than a EF bug. I'm sure someone would have noticed a DB that you can't update.
推荐答案
事实证明,更改数据库的生命周期我在测试夹具中的DbContext单例解决了我的问题。
It turned out that changing the lifetime of my DbContext in my test fixture to singleton solved my issue.
这篇关于EF Core 2.1内存DB中不更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!