来自类库项目的迁移未应用于 SQL Server 数据库

问题描述

我的解决方案有 3 个项目。它们是:

enter image description here

DevIO.App认的 MVC 项目。 DevIO.BusinessDevIO.Data 是 .NET Core 类库,前者有 3 个模型,后者是这些模型的迁移以及 DbContext 的实现:

enter image description here

然后,我准备了 DevIO.AppStartup 文件,其中包含来自 DevIO.Data 的上下文:

public void ConfigureServices(IServiceCollection services)
{
    // This was generated when I created the project,but it came with sqlite. I added DevIO.Data as a project reference into DevIO.App and changed it to use sql Server after.
    // I guess it is using UsesqlServer from this reference because I did not add any package regaring sql Server to DevIO.App
    services.AddDbContext<ApplicationDbContext>(options => options.UsesqlServer(Configuration.GetConnectionString("DefaultConnection")));

    // The line I added manually
    services.AddDbContext<DevIoDbContext>(options => options.UsesqlServer(Configuration.GetConnectionString("DefaultConnection")));

    // ...
}

为了从 DevIO.App 内运行身份迁移,我cd进入文件夹并运行:

dotnet ef 数据库更新 --context ApplicationDbContext

因此,已经创建了迁移:

enter image description here

但是,尽管有成功的消息,但并未在数据库中创建来自 DevIO.Data 的迁移:

gabriel in ASP.NET-MVC-Core/src/DevIO.App took 8s 
➜ dotnet ef database update --context DevIoDbContext
Build started...
Build succeeded.
Done.

我还尝试从 .sln 文件的位置执行以下操作,但结果相同。

dotnet ef --startup-project src/DevIO.App 数据库更新 --context DevIoDbContext

我尝试指定不同的启动项目,但随后我会看到错误

无法创建类型为“DevIoDbContext”的对象。

奇怪的是,当运行命令以显示 DevIoDbContext 的脚本时,它只会显示sql

IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
    CREATE TABLE [__EFMigrationsHistory] (
        [MigrationId] nvarchar(150) NOT NULL,[ProductVersion] nvarchar(32) NOT NULL,CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
    );
END;
GO

甚至没有合适的桌子。有些东西关闭了,但我想不通。

其他尝试包括跑步

dotnet ef 数据库更新 --project src/DevIO.Data --startup-proect src/DevIO.App --context DevIoDbContext

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)