2012-01-13 159 views
8

我GOOGLE了這一點,並沒有得到任何答案,我的特殊情況。序列包含不止一個匹配的元素 - 添加項與Entiity框架

This shows the exceptions i'm getting.

林在suposedly簡單的方式使用實體框架。我正在嘗試向Memberproduct表中添加一條記錄。不過,我收到了一個沒有道理的例外。

任何想法,這裏有什麼錯?

MemberProduct類:

public class MemberProduct :ISaleable 
{ 
    public void ProcessSale() 
    { 
     throw new NotImplementedException(); 
    } 

    private int id { get; set; } 
    private string productName { get; set; } 
    private decimal price { get; set; } 
    private TaxClass taxClass { get; set; } 
    private int quantity { get; set; } 
    private Member memberAssociation { get; set; } 

    public TaxClass TaxClass 
    { 
     get 
     { 
      return this.taxClass; 
     } 
     set 
     { 
      this.taxClass = value; 
     } 
    } 
    public int Quantity 
    { 
     get 
     { 
      return this.quantity; 
     } 
     set 
     { 
      this.quantity = value; 
     } 
    } 
    public string ProductName 
    { 
     get 
     { 
      return this.productName; 
     } 
     set 
     { 
      this.productName = value; 
     } 
    } 
    public decimal Price 
    { 
     get 
     { 
      return this.price; 
     } 
     set 
     { 
      this.price = value; 
     } 
    } 
    public Member MemberAssociation 
    { 
     get 
     { 
      return this.memberAssociation; 
     } 
     set 
     { 
      this.memberAssociation = value; 
     } 
    } 
    public int ID 
    { 
     get 
     { 
      return this.id; 
     } 
     set 
     { 
      this.id = value; 
     } 
    } 
} 

堆棧跟蹤:

 at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) 
    at System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.IdKeyDiscoveryConventionImpl.MatchKeyProperty(EdmEntityType entityType, IEnumerable`1 primitiveProperties) 
    at System.Data.Entity.ModelConfiguration.Conventions.KeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType entityType, EdmModel model) 
    at System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType entityType, EdmModel model) 
    at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch[TEdmDataModelItem](TEdmDataModelItem item) 
    at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType item) 
    at System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod) 
    at System.Data.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace, IEnumerable`1 entityTypes) 
    at System.Data.Edm.Internal.EdmModelVisitor.VisitEdmNamespace(EdmNamespace item) 
    at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace item) 
    at System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod) 
    at System.Data.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel model, IEnumerable`1 namespaces) 
    at System.Data.Edm.Internal.EdmModelVisitor.VisitEdmModel(EdmModel item) 
    at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmModel(EdmModel item) 
    at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch() 
    at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModel(EdmModel model) 
    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.InternalContext.Initialize() 
    at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) 
    at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() 
    at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() 
    at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) 
    at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) 
    at System.Data.Entity.DbSet`1.Add(TEntity entity) 
    at Nautix_EPOS.Controllers.HomeController.Index() in C:\sites\EPOS\Nautix EPOS\Nautix EPOS\Controllers\HomeController.cs:line 19 
    at lambda_method(Closure , ControllerBase , Object[]) 
    at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) 
    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) 
+0

什麼是堆棧跟蹤? – SLaks 2012-01-13 19:10:06

+0

哇,如果你沒有發佈截圖,我不會相信你 – 2012-01-13 19:10:59

+0

你可以發佈'MemberProduct'的代碼嗎? – nemesv 2012-01-13 19:17:06

回答

8

我可以攝製您的問題。您的MemberProduct具有 Id屬性具有不同殼體:在映射期間

public class MemberProduct 
{ 
    public int Id { get; set; } 

    public int ID { get; set; } 
} 

EF代碼首先使用約定。其中一個約定的是,它把命名IdTypenameId作爲主鍵的屬性(如果你不使用Key屬性或自定義映射),也因爲它的屬性名稱比較不區分大小寫的情況下,它拋出異常。

刪除的特性之一,它應該工作。

+0

如何使用鍵屬性?由於ID屬性是從接口繼承的,因此必須要有ID。 – gunwin 2012-01-13 19:31:35

+0

你可以找到Key的例子,例如[here](http://stackoverflow.com/questions/5482670/entity-framework-code-first-define-the-key-for-this-entitytype)。但我認爲你在這裏濫用屬性:而不是私有屬性使用私有字段:'private int id {get;組; }'更改爲'私人詮釋ID;'等,而且它也應該工作。 – nemesv 2012-01-13 19:35:44

+0

或者如果您不打算將邏輯放在屬性訪問器中,請使用plain [auto properies](http://msdn.microsoft.com/zh-cn/library/bb384054.aspx)並讓框架生成專用字段爲你。 – nemesv 2012-01-13 19:40:19

1

我懷疑EF是越來越由兩個ID屬性混淆。

將您的私人財產更改爲字段;應該修復它。

0

我設置實體狀態信息不正確時,在最初播種得到這個順序錯誤。只要確保你正確設置了對象圖的狀態。我的問題是種子數據(不是在實際的應用程序運行期間)...無論如何,這是我的答案,如果它可以幫助任何人在運行更新時遇到序列錯誤(如「序列包含多個匹配元素」) -database命令進行遷移。當你在種子方法外面得到這個錯誤時,我認爲你的答案可能在於正確設置對象圖的實體狀態。

Entity Framework Code First AddOrUpdate method insert Duplicate values

要使用正確設置實體對象圖得到幫助,請參閱以下博客文章這是我找到的正確設置對象狀態最好的:

http://blog.longle.net/2013/05/11/genericizing-the-unit-of-work-pattern-repository-pattern-with-entity-framework-in-mvc/

相關問題