Nahro Fuad
Nahro Fuad

Reputation: 821

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"?

Upvotes: 82

Views: 62053

Answers (10)

Kundan Kumar
Kundan Kumar

Reputation: 61

If you're facing this type of error in Visual Studio 2022 Entity Framework: Value cannot be null. Parameter name: type

follow these steps:-

Step 1:- Update your EF to Version 6.4.4

Step 2:- Restart your application

Step 3:- Run your migration command

It works for me.

Upvotes: 6

Coden
Coden

Reputation: 2868

For those who do have this problem using Visual Studio 2022, there are at least two fixes:

  • Switch back to Visual Studio 2019, as it doesn't work yet in the 2022 version if you are using an older Entity Framework version.

  • Update Entity Framework to 6.4.4 to resolve it

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.

Upvotes: 163

Shahram
Shahram

Reputation: 25

Update entityframework to version 6.4.4 and then Use EntityFramework6\Add-Migration command, microsoft have changed name of cmdmidlet

Its work from me

Upvotes: 1

Atif
Atif

Reputation: 327

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

Upvotes: 2

diego.escada
diego.escada

Reputation: 166

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"

Upvotes: 3

Pratik Girme
Pratik Girme

Reputation: 507

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.

Upvotes: 36

tmaj
tmaj

Reputation: 34947

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.

Upvotes: 0

Venugopal M
Venugopal M

Reputation: 2412

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.

Upvotes: 1

fatderda
fatderda

Reputation: 81

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.

Upvotes: 7

Iman Mahmoudinasab
Iman Mahmoudinasab

Reputation: 7015

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.

Upvotes: 12

Related Questions