使用EF Core的CodeFirt 出现的问题The specified framework version '2.1' could not be parsed

今天使用了一下EF Core的Code First,进行数据库迁移的的时候报错了:

The specified framework version '2.1' could not be parsed

解决方法:在DbContext的csporj工程文件里指定一下运行时框架

 <RuntimeFrameworkVersion>2.1.6</RuntimeFrameworkVersion>

添加完成后再次运行迁移指令:dotnet ef migrations add <记录名>

结果又爆出了新的错误

Unable to create an object of type 'EFDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<EFDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.

出现这种错误是因为Dbcontext没有在Web项目里面,所以他找不到链接字符串,没办法实例化DbContext

解决方案:我们在Dbcontext的同级目录新建一个类DesignTimeDbContextFactory

让这个类继承IDesignTimeDbContextFactory接口

 

写完后再次执行迁移指令,发现迁移成功

出现这种问题,我个人认为应该是我的DbContext没有建到Web项目下的原因,我觉得如果引入一下Nuget的Asp.NETMVC包应该也能解决问题,不过我就不去试了哈哈哈哈哈

原文地址:https://www.cnblogs.com/Tassdar/p/10037594.html