EntityFramework Enable-Migrations report exceptions “No context type was found in the assembly”

In the past, the project did not use the form of Classification Library, so the migration was very smooth, and there was no situation

The project is a little big this time, so we have to classify the library to facilitate the development and maintenance

An exception occurred while enabling project entityframework migration in the solution

Exception said that the dbcontext class was not found in my project

This dbcontext class is not really placed under the startup project, but a separate class library is established to store it

It was referenced in the startup project but could not be found

Check the get help enable migrations help and find that the enable migration command takes several parameters

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

Contexttypename: the class name that the project inherits from dbcontext

Enable automatic migrations: enables automatic migration

ProjectName: the name of the project where the dbcontext class is stored

Startupprojectname: the name of the startup project in the solution. It is used to call the connection string under the project

Connectionstringname: connection string name

The above five parameters are necessary to solve the problem, and the others are irrelevant

For example:

Enable-Migrations -ContextTypeName “DBAccessLib.TJSSDBContext” -ProjectName “DBAccessLib” -StartUpProjectName “WebSite” -ConnectionStringName “TJSSDBContext” -Verbose

After filling in the order, the problem is solved

Similarly, you need to fill in the corresponding parameters when you add migration or update database. Otherwise, the same error will occur

For example:

Add-Migration -Name “EditCST_ DevicePhoto” -ProjectName “DBAccessLib” -StartUpProjectName “WebSite” -ConnectionStringName “TJSSDBContext” -Verbose

Update-Database -Script -ProjectName “DBAccessLib” -StartUpProjectName “WebSite” -ConnectionStringName “TJSSDBContext” -Verbose

Similar Posts: