【问题标题】:Error with Entity Framework add-migration: "value cannot be null"实体框架添加迁移出错:“值不能为空”
【发布时间】:2014-05-19 10:17:13
【问题描述】:

我正在开发一个 mvc4 asp.net Web 应用程序。我想添加一个迁移,但显示错误,我不知道如何解决它。 这是该类的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace POCOs
{
public class Consultant
{
public int Id { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public string RefCommercial { get; set; }
public string DateNaissance { get; set; }
public int CIN { get; set; }
public string Fonction { get; set; }
public string Tel { get; set; }
public string Experience { get; set; }
public string Email { get; set; }
public HttpPostedFileBase CV { get; set; }
public ICollection<Prestation> prestations { get; set; }

public Consultant()
{
prestations = new HashSet<Prestation>();
}
}
}

这是我想添加迁移时遇到的错误:

Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: cv
System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.SortedEntityTypeIndex.Add(EdmEntitySet entitySet, EdmEntityType entityType)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Analyze()
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Value cannot be null.
Parameter name: key 

有什么帮助吗?

【问题讨论】:

标签: c# entity-framework


【解决方案1】:

[NotMapped] 属性放在 HttpPostedFileBase 上

看这个:

Value cannot be null. Parameter name: extent

【讨论】:

    【解决方案2】:

    我在 EF7 中工作。我也遇到了这个错误,不知道是什么原因造成的。

    最终我发现我做了一些命名空间重构,所以我的模型类与快照类位于不同的命名空间(快照类使用字符串来限定类!)

    我删除了快照文件并进行了虚拟迁移以更新快照。然后我删除了虚拟迁移,此后我的迁移再次正常工作。

    【讨论】:

    • 感谢解决了我的错误,由于我的粗心,命名空间错误
    • 谢谢老兄,你解决了我这几天一直在研究的问题。我对此很生气。
    • 重建快照解决了我的问题 - 显然快照文件中的事物顺序很重要,并且当开发人员合并他们的更改时它已经混淆了。
    【解决方案3】:

    HttpPostedFileBase 是 System.Web 命名空间中的内置类。它不会被 EF 识别。你可以创建一个包装类型来持久化你需要的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 2020-10-23
      • 2019-02-25
      • 1970-01-01
      • 2012-03-17
      • 2015-05-11
      相关资源
      最近更新 更多