An error occurred while accessing the Microsoft.Extensions.Hosting services.Error: 无法打开登录所请求的数据库 “..

数据库迁移问题

image-20240615202234613

第一步,我们选中有DbContext的项目,然后执行迁移命令:Add-MigrationorUpdate-DataBase。你会看到控制台报这样的错误:

image-20240615203335552
Your startup project 'FileService.Infrastructure' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

第二步,我们就按照报错的提示,给该项目添加Microsoft.EntityFrameworkCore.Design依赖包。然后再执行迁移命令,你会看到下面这样的报错信息:

image-20240615203507301

你可以自己通过后面那个提示链接去查看解决方案,或者按照下面这种方案。

第三步,添加对项目的引用:

image-20240615204412499

第四步,在当前项目下添加一个继承于IDesignTimeDbContextFactory的类,代码如下:

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<FSDbContext>
{
    public FSDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = DbContextOptionsBuilderFactory.Create<FSDbContext>();
        return new FSDbContext(optionsBuilder.Options, null);
    }
}

上述代码是根据项目代码结构进行的改写,可以根据自己的需求将代码改编。

第四步,执行数据迁移命令,成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值