2012-01-13 154 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/

相关问题