【发布时间】:2020-11-24 11:14:14
【问题描述】:
我为一个新的 ASP.Net Core 应用创建了 2 个 DbContexts。
它们都派生自同一个 ApplicationDbContext 基类,唯一的区别是 OnConfiguring 覆盖,其中使用 UseMySql 而不是 UseSqlite,遵循 post about multiple providers 中的建议。
SqliteDbContext.cs 有这个:
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
#if DEBUG_EF
options.UseSqlite("DataSource=");
#endif
}
MySqlDbContext.cs 有这个:
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
#if DEBUG_EF
options.UseMySql("Server=");
#endif
}
运行了以下命令:
dotnet ef migrations add InitialCreate --context MySqlDbContext --output-dir Migrations/MySql --configuration DebugEf
返回的错误是:
无法创建“MySqlDbContext”类型的对象。对于不同的 设计时支持的模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728
但是,以下使用 Sqlite 上下文的命令是成功的。
dotnet ef migrations add InitialCreate --context SqliteDbContext --output-dir Migrations/Sqlite --configuration DebugEf
【问题讨论】:
-
请不要链接到属于该问题的主要代码的外部存储库。如果链接断开,问题应该仍然可以理解。
-
对于链接中断或(更有可能)链接代码发生剧烈变化的问题来说,这还不足以解决问题。
-
存储库是专门为这个问题制作的。这不是我需要修复的实际项目。
-
不过,所有 Stack Overflow 问题都应该是独立的。
标签: asp.net entity-framework asp.net-core