75

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

Joshua
  • 40,822
  • 8
  • 72
  • 132
Nahro Fuad
  • 751
  • 1
  • 5
  • 5

10 Answers10

146

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.

Coden
  • 2,579
  • 1
  • 18
  • 25
  • 18
    I upgraded ef 6.2 to 6.4, my problem solved. thanks! – Majid Basirati Dec 21 '21 at 07:22
  • 1
    Arthur Vickers is not a "project member"... he's the Principal Software Engineering Manager for the Entity Framework team at Microsoft. – JHBonarius Jan 06 '22 at 13:33
  • ` 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` If anyone is in that situation. I confirmed, that it is possible to create the migrations in a separate branch with EF 6.4.4. and merge it into the main branch still on version 6.1.3. – Jürgen Steinblock Jan 10 '23 at 09:02
  • For me the trick was to upgrade ALL PROJECTS in my solution to 6.4.4 then restart Visual Studio – pjaaar Aug 28 '23 at 14:37
30

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.

Pratik Girme
  • 437
  • 4
  • 4
12

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.

Iman Mahmoudinasab
  • 6,861
  • 4
  • 44
  • 68
  • You can avoid having to set this each time by calling the update-database command with the --StartupProjectName param. 'update-database -ProjectName B2B.Data -StartupProjectName B2B.UI.Web' – Greg Jun 24 '22 at 04:27
7

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.

fatderda
  • 81
  • 1
  • 3
3

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"
diego.escada
  • 166
  • 1
  • 4
1

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.

Venugopal M
  • 2,280
  • 1
  • 20
  • 30
1

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

Atif
  • 278
  • 1
  • 3
  • 16
1

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

Its work from me

Shahram
  • 27
  • 4
0

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.

tymtam
  • 31,798
  • 8
  • 86
  • 126
0

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.

Mr. Kabir
  • 695
  • 8
  • 9