'Entity Framework : Value cannot be null. Parameter name: type

When I execute update-database command, it shows this error message

System.ArgumentNullException: Value cannot be null.
Parameter name: type

at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project project, Int32 shellVersion)
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebSiteProject(Project project)
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetTargetDir(Project project)
at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory)
at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

Value cannot be null.
Parameter name: type"?



Solution 1:[1]

For those who do have this problem using Visual Studio 2022: Switch back to Visual Studio 2019, as it doesn't work yet in the 2022 version when you are using an older Entity Framework version.

On the relevant issue file on the Entity Framework 6 GitHub repository, project member ajcvickers commented on 2021-11-18:

[...] EF 6.2 doesn't work. You will need to update to EF 6.4.4. We have so far been unable to reproduce this with EF 6.4.4.

While numerous users have reported upgrading to EF 6.4.4 resolves their problem, the issue is still open, as there are users who can't downgrade to Visual Studio 2019 or upgrade Entity Framework, as those changes could break pipelines.

Solution 2:[2]

Solution: For me solution was as easy as restarting visual studio.

Senario: I have solution like this:

solution explorer

I changed startup project from Host to the UI.Web project which contains web.config and then running the update-database command but it gave me the Value cannot be null. exception until I restart visual studio and rerun the update-database command. This time it behaved normally.

Solution 3:[3]

I got this error in VS 2022, while I was using EF version 6.1.3. I upgraded EF to version 6.4.4 and the issue got resolved. The .Net version was 4.8.

Solution 4:[4]

Had exactly this error message (Value cannot be null. (Parameter 'type')) when I added a migration. Tried the other solutions (restart VS) but they didn't work.

After a long time searching I found the source of the problem. I made a mistake when setting the 'InverseProperty' annotation in one of my POCOs:

public class Workflow{
 [InverseProperty("Workflow")]
 public List<Node> Nodes { get; set; } 
 ...
}
public class Node{
 public int WorkflowId{ get; set; } 
 public Workflow Workflow{ get; set; } 
 ...
}

instead of

public class Workflow{
 [InverseProperty("WorkflowId")] //needs to be Workflow instead of WorkflowId
 public List<Node> Nodes { get; set; } 
 ...
}

I wished the EF Exception would have been a bit more precise.

Solution 5:[5]

An alternative solution in VS 2022 without having to install VS 2019 is to use Migrate.exe, here it explains how to use it. It is more or less like this:

Migrate.exe your_app_ef_model.dll /startupConfigurationFile="your_app.config" /targetMigration="migrationName"

Solution 6:[6]

This issue is most commonly raised when the ApplicationDbContextModelSnapshot file in the Migrations folder no longer matches the entity relationship.

Perhaps deleting this file and running the migration could solve the problem. Anyways, this file is again created while add-migration command is executed.

Solution 7:[7]

In my case the issue was that my ip address has changed and the target database - behind a firewall - was no longer accepting connections from my machine.

For this flavour of the problem the solution is changing the firewall settings or fixing any other connection issues.

Solution 8:[8]

I had the same error in MVC 5 in VS 2022 but when I run it on VS 2019. That fixed my error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Iman Mahmoudinasab
Solution 3 Pratik Girme
Solution 4 fatderda
Solution 5 diego.escada
Solution 6 Venugopal M
Solution 7 tymtam
Solution 8 Atif