实体框架:值不能为空。参数名称:类型

时间:2017-01-21 09:13:20

标签: entity-framework types null

执行update-database命令时,会显示此错误消息

  

System.ArgumentNullException:值不能为空。
  参数名称:type

     

在System.Activator.CreateInstance(Type type,BindingFlags bindingAttr,Binder binder,Object [] args,CultureInfo culture,Object [] activationAttributes)
  在System.Activator.CreateInstance(Type type,Object [] args)
  在System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(项目项目,Int32 shellVersion)
  在System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebSiteProject(项目项目)
  在System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetTargetDir(项目项目)
  在System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName,Boolean useContextWorkingDirectory)
  at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name,Boolean force,Boolean ignoreChanges)
  在System.Data.Entity.Migrations.AddMigrationCommand。<> c__DisplayClass2。< .ctor> b__0()
  在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(操作命令)

     

值不能为空。
  参数名称:输入“?

5 个答案:

答案 0 :(得分:7)

解决方案:对我而言,解决方案就像重新启动visual studio一样简单。

Senario:我有这样的解决方案:

solution explorer

我将启动项目从Host更改为包含web.config的UI.Web项目,然后运行update-database命令但是它给了我Value cannot be null.例外,直到我重新启动visual studio并重新运行update-database命令。这次它表现正常。

答案 1 :(得分:7)

添加迁移时,完全显示此错误消息(Value cannot be null. (Parameter 'type'))。尝试了其他解决方案(重新启动VS),但是它们不起作用。

经过长时间的搜索,我找到了问题的根源。 在其中一个POCO中设置“ InverseProperty”注释时,我犯了一个错误:

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

代替

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

我希望EF异常会更加精确。

答案 2 :(得分:0)

我重命名了上下文类,问题已经解决了。我不知道为什么。

答案 3 :(得分:0)

当Migrations文件夹中的ApplicationDbContextModelSnapshot文件不再与实体关系匹配时,通常会出现此问题。

也许删除此文件并运行迁移可以解决问题。无论如何,该文件是在执行add-migration命令时再次创建的。

答案 4 :(得分:0)

就我而言,问题是我的 IP 地址已更改,并且目标数据库(位于防火墙后面)不再接受来自我的机器的连接。

对于这种问题,解决方案是更改防火墙设置或修复任何其他连接问题。

相关问题