2014-05-19 90 views
1

我正在使用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 

任何幫助嗎?

+1

您的HttpPostedFileBase屬性可能是問題 - EF將映射到哪個數據類型?您可以使用[NotMapped]數據註解來排除它:[http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute%28v=vs.110%29.aspx]( http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.notmappedattribute%28v=vs.110%29.aspx) – markpsmith

+0

爲了澄清,這實際上是一個實體框架問題。 ASP.NET MVC純粹是一種Web技術; ASP.NET項目默認包含Entity Framework來處理數據庫訪問。我已經相應地編輯了標籤。 – anaximander

回答

1

HttpPostedFileBase是的System.Web命名一個內置的類。它不會被EF認可。你可以創建一個包裝類型來保存你需要的數據。

3

我正在EF7工作。我也有這個錯誤,不知道是什麼原因造成的。

最後,我跟蹤它的事實,我已經做了一些命名空間的重構,所以我的模型類是在不同的命名空間比他們在快照類(快照類使用字符串來限定類!)

我刪除了快照文件並進行了虛擬遷移以更新快照。然後我刪除了虛擬遷移,然後我的遷移再次正常工作。

+0

感謝解決了我的錯誤,由於我的粗心,命名空間是錯誤的 – Sherlock

+0

謝謝兄弟,你解決了我的問題,我正在工作幾天。我對此感到生氣。 – Skorek

+0

重建快照解決了我的問題 - 顯然快照文件中事物的順序很重要,當開發人員合併其更改時,它變得混亂起來。 – ghosttie