序列包含一个以上的匹配元素 - 添加项目与Entiity框架 [英] Sequence contains more than one matching element - Adding item with Entiity Framework

查看:4888
本文介绍了序列包含一个以上的匹配元素 - 添加项目与Entiity框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我GOOGLE了这一点,并没有得到任何答案,我的特殊情况。

林在suposedly简单的方式使用实体框架。我想一个记录添加到Memberproduct表。不过,我发现了一个异常没有任何意义。

任何想法有什么不对吗?

MemberProduct类:

 公共类MemberProduct:ISaleable
{
    公共无效ProcessSale()
    {
        抛出新的NotImplementedException();
    }

    私人诠释编号{获得;组; }
    私人字符串产品名称{获得;组; }
    私人十进制价格{获得;组; }
    私人TaxClass taxClass {获得;组; }
    私人诠释数量{获得;组; }
    私人会员memberAssociation {获得;组; }

    公共TaxClass TaxClass
    {
        得到
        {
            返回this.taxClass;
        }
        组
        {
            this.taxClass =价值;
        }
    }
    公众诠释数量
    {
        得到
        {
            返回this.quantity;
        }
        组
        {
            this.quantity =价值;
        }
    }
    公共字符串产品名称
    {
        得到
        {
            返回this.productName;
        }
        组
        {
            this.productName =价值;
        }
    }
    公共十进制价格
    {
        得到
        {
            返回this.price;
        }
        组
        {
            this.price =价值;
        }
    }
    公共成员MemberAssociation
    {
        得到
        {
            返回this.memberAssociation;
        }
        组
        {
            this.memberAssociation =价值;
        }
    }
    公众诠释ID
    {
        得到
        {
            返回this.id;
        }
        组
        {
            this.id =价值;
        }
    }
}
 

堆栈跟踪:

 在System.Linq.Enumerable.SingleOrDefault [TSource](IEnumerable`1源,Func`2 predicate)
   在System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.IdKeyDiscoveryConventionImpl.MatchKeyProperty(EdmEntityType的EntityType,IEnumerable`1 primitiveProperties)
   在System.Data.Entity.ModelConfiguration.Conventions.KeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType的EntityType,EdmModel模型)
   在System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Edm.EdmEntityType>.Apply(EdmEntityType的EntityType,EdmModel模型)
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch[TEdmDataModelItem](TEdmDataModelItem项目)
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType项目)
   在System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection [T](IEnumerable`1收集,Action`1 visitMethod)
   在System.Data.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace,IEnumerable`1 entityTypes)
   在System.Data.Edm.Internal.EdmModelVisitor.VisitEdmNamespace(EdmNamespace项)
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace项目)
   在System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection [T](IEnumerable`1收集,Action`1 visitMethod)
   在System.Data.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel模型,IEnumerable`1命名空间)
   在System.Data.Edm.Internal.EdmModelVisitor.VisitEdmModel(EdmModel项)
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmModel(EdmModel项目)
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch()
   在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModel(EdmModel模型)
   在System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)
   在System.Data.Entity.DbModelBuilder.Build(的DbConnection providerConnection)
   在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   在System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput输入)
   在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   在System.Data.Entity.Internal.InternalContext.Initialize()
   在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型的EntityType)
   在System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   在System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   在System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(动作动作,的EntityState newState,对象实体,字符串方法名)
   在System.Data.Entity.Internal.Linq.InternalSet`1.Add(对象实体)
   在System.Data.Entity.DbSet`1.Add(TEntity实体)
   在Nautix_EPOS.Controllers.HomeController.Index()在C:\网站\ EPOS \ Nautix EPOS \ Nautix EPOS \控制器\ HomeController.cs:行19
   在lambda_method(封闭,ControllerBase,对象[])
   在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,对象[]参数)
   在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2参数)
   在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2参数)
   在System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
   在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter过滤器,ActionExecutingContext preContext,Func`1续)
 

解决方案

我可以瑞普您的问题。你的 MemberProduct 两个 编号性质不同的外壳:

 公共类MemberProduct
{
    公众诠释编号{获得;组; }

    公众诠释ID {获得;组; }
}
 

EF code首先使用约定的映射过程。其中一个约定是,它把命名属性编号 TypenameId 作为主键(如果你不使用属性或自定义映射),也因为它的属性名称比较不区分大小写的情况下就抛出异常。

删除特性之一,它应该工作。

I've googled this and don't get any answers for my particular circumstance.

Im using Entity Framework in a suposedly simple way. I'm trying to add a record to the Memberproduct table. However I'm getting an exception that doesn't make sense.

Any ideas what's wrong here?

MemberProduct Class:

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

Stack Trace:

     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)

解决方案

I could repro your issue. Your MemberProduct has two Id properties with different casing:

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

    public int ID { get; set; }
}

EF code first uses conventions during the mapping. One of the convention is that it treats properties named Id or TypenameId as primary keys (if you don't use the Key attribute or custom mapping) and because it does the property name comparison case insensitively it throws the exception.

Remove one of properties and it should work.

这篇关于序列包含一个以上的匹配元素 - 添加项目与Entiity框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆