No context type was found in the assembly 'xxx.xxxx'. CodeFirst Ef错误

最简单的解决方案是将启动项目设置为你要生产Migration的项目。

例如:

我这边将Try.EntityFramework设置为启动项目。并且准备在该项目下生成Migration文件。这里的默认项目同样设置为Try.EntityFramework。就OK了。

2.另一种方案。

查看get-help Enable-Migrations帮助,发现启用迁移命令带了几个参数。

Enable-Migrations [-ContextTypeName <String>] [-EnableAutomaticMigrations] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConnectionStringName <String>] [-Force] [<CommonParameters>]

ContextTypeName:项目继承自DBContext的类名字。

EnableAutomaticMigrations:开启自动迁移。

ProjectName:存放DBContext类的项目名称。

StartUpProjectName:解决方案中启动项目的名称,作用是调用该项目下的连接字符串。

ConnectionStringName:连接字符串名称

上面五个参数是解决问题必须的,其它的无关紧要。

例如:

Enable-Migrations -ContextTypeName "Try.EntityFramework.TryDbContext" -ProjectName "Try.EntityFramework" -StartUpProjectName "Try.EntityFramework" -ConnectionStringName "Default" -Verbose

依次填好之后,问题解决。

同样的在Add-Migration、Update-Database的时候也需要填写相应的参数。否则会出现同样错误。

例如:

add-migration -Name "InitDb" -ProjectName "Try.EntityFramework" -StartUpProjectName "Try.EntityFramework" -ConnectionStringName "Default" -Verbose

update-database -script -ProjectName "Try.EntityFramework" -StartUpProjectName "Try.EntityFramework" -ConnectionStringName "Default" -Verbose

原文地址:https://www.cnblogs.com/FlyStupidBird/p/8944782.html