为了设置用于测试的TestContainer的MsSqlTestcontainer,我复制了dotnet ef migrations script发出的脚本并将其植入单元测试的设置中(实际代码没有:
void PrepareDb(MsSqlTestcontainer dbTestContainer){
dbTestContainer.ExecScriptAsync(@"CREATE TABLE [DbName] ( /* ... */ )");
}是否有一种方法可以使其自动化,例如,如果DB模型发生变化,并将其连接起来,例如MyDbContext直接连接到TestContainer的逻辑中?
我正在考虑将MyDbContext代码传递到容器中,并在其中运行dotnet ef migrations script,但我不确定它是否值得这么做(而且我需要使用一个已经安装了dotnet的容器,这是另一个复杂的问题)。
这是dbTestContainer设置,FWIW:
var dbTestContainer = new TestcontainersBuilder<MsSqlTestcontainer>()
.WithDatabase(new MsSqlTestcontainerConfiguration { Password = "whatever secret" })
.WithImage("mcr.microsoft.com/azure-sql-edge")
.WithWaitStrategy(Wait.ForUnixContainer())
.Build()发布于 2022-09-20 17:23:40
连接到容器数据库,并在测试开始之前以编程方式运行迁移。
dbContext.Database.Migrate();
https://stackoverflow.com/questions/73790453
复制相似问题