Jiji
Jiji

Reputation: 113

Error with Entity Framework add-migration: "value cannot be null"

i am working on an mvc4 asp.net web application. I wanted to add a migration but an error shows on and I don't know how to resolve it. Here's the code of the class:

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>();
}
}
}

and here's the error I get when I want to add a migration:

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 

Any help?

Upvotes: 1

Views: 11112

Answers (3)

Shaul Behr
Shaul Behr

Reputation: 38033

I am working in EF7. I also got this error, and had no idea what was causing it.

Eventually I tracked it down to the fact that I'd done some namespace refactoring, so my model classes were in different namespaces than they were in the snapshot class (the snapshot class uses strings to qualify the classes!)

I deleted the snapshot file and made a dummy migration to update the snapshot. Then I deleted the dummy migration and thereafter my migrations worked properly again.

Upvotes: 5

Hussein Zawawi
Hussein Zawawi

Reputation: 2937

HttpPostedFileBase is a build-in class in System.Web namespace. It wont be recognized by the EF. you can create a wrapper type to persist the data you need it.

Upvotes: 1

stackunderflow
stackunderflow

Reputation: 3883

Put [NotMapped] attribute on HttpPostedFileBase

See this:

Value cannot be null. Parameter name: extent

Upvotes: 7

Related Questions