C# EF之No context type was found in the assembly ' '.

解决方案中启用项目EntityFramework迁移时却发生了异常。

异常说在我的项目中没有找到DBContext类。

这个DBContext类确实没有放在启动项目下面,是另外建立了一个独立的类库来存放。

查看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 -ProjectName CodeFirst.Factory -StartUpProjectName CodeFirst -Verbose  //此时默认项目是CodeFirst

继续输入“add-migration createdb”,//此时默认项目改为CodeFirst.Factory

最后一步输"update-database"

没有报错就是成功了

原文地址:https://www.cnblogs.com/liujianshe1990-/p/13189083.html