84

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

1
  • 4
    this a bug in vs2022 . try with vs 2019 Commented Sep 12, 2022 at 7:08

10 Answers 10

168

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I upgraded ef 6.2 to 6.4, my problem solved. thanks!
Arthur Vickers is not a "project member"... he's the Principal Software Engineering Manager for the Entity Framework team at Microsoft. 😅
` 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.
For me the trick was to upgrade ALL PROJECTS in my solution to 6.4.4 then restart Visual Studio
Updating (6.5.1 now) worked for me. Thanks.
36

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.

1 Comment

This repeats the answer from Apfelkuacha a month ago.
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.

1 Comment

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'
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.

Comments

5

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.

1 Comment

To get this work i had to upgrade EF from 6.4.4. to 6.5.1
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"

Comments

2

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

1 Comment

thank you, it worked for me when i switched from vs2022 to 2019 just to add migration
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.

1 Comment

Note that this answer applies to EntityFramework Core specifically. EntityFramework doesn't have such snapshot files.
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.

Comments

0

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

Its work from me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.