【发布时间】:2020-11-17 14:33:59
【问题描述】:
.NET Core 3.1、EF Core 5.0 预览版 6。
我使用Simple Injector 5.0注册IMyContextFactory(我没有使用services.AddDbContext<>())。
我的MyContext 将所有构造函数设置为私有,因为我使用没有tenantId 的ContextFactory,有时在一个请求中使用几个不同的tenantId,所以我确保每个人都将使用IMyContextFactory 注入而不是MyContext 并防止创建new MyContext() 在控制器等中。
但是Update-Database怎么办?
- 虽然我执行
Update-Database时总是出现错误Login failed for user ''.,但我可以毫无问题地从我的WebAPI 项目运行并连接到数据库(我的ConnectionString 很好)。 - 我能够在运行时应用迁移,但我必须添加
services.AddDbContext<MyContext>(),将MyContext的构造函数设为公开,并从Program.cs 中的scope.ServiceProvider.GetRequiredService<MyContext>()获取此上下文,因为我的IMyContextFactory不在里面ServiceProvider。 (通过https://docs.microsoft.com/pl-pl/ef/core/managing-schemas/migrations/applying?tabs=vs#apply-migrations-at-runtime)
如何仅使用 Simple Injector 来实现?
【问题讨论】:
-
你试过design-time DbContext factory吗?我不确定
Update-Database是否使用它,但也许可以。 -
是的,我已经实现了它。但是,是的,
Update-Database使用它。谢谢:)
标签: c# asp.net-core entity-framework-core entity-framework-5 simple-injector