序列包含多个匹配元素 - 使用Entiity Framework添加项 [英] Sequence contains more than one matching element - Adding item with Entiity Framework

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

问题描述





我以一种简单的方式使用Entity Framework。我正在向Memberproduct表添加一条记录。但是我收到一个没有意义的例外。



任何想法在这里有什么问题?



MemberProduct类:

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

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

public TaxClass TaxClass
{
get
{
return this.taxClass;
}
set
{
this.taxClass = value;
}
}
public int数量
{
get
{
return this.quantity;
}
set
{
this.quantity = value;
}
}
public string ProductName
{
get
{
return this.productName;
}
set
{
this.productName = value;
}
}
公共小数价格
{
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;
}
}
}

堆栈跟踪:在System.Linq.Enumerable.SingleOrDefault [TSource](IEnumerable`1源,Func`2谓词)中

  
在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 model)
在System.Data.Entity.ModelConfiguration.Conventions.IdKeyDiscoveryConvention.System.Data.Entity.ModelConfiguration。 Conventions.IEdmConvention< System.Data.Edm.EdmEntityType> .Apply(EdmEntityType entityType,EdmModel model)
在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispa tch [TEdmDataModelItem](TEdmDataModelItem项)
在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType项)
在System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection [T ](IEnumerable`1 collection,Action`1 visitMethod)
在System.Data.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace,IEnumerable`1 entityTypes)
在System.Data.Edm.Internal。 EdmModelVisitor.VisitEdmNamespace(EdmNamespace item)
在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace item)
在System.Data.Edm.Internal.DataModelItemVisitor.VisitCollection [T] (IEnumerable`1 collection,Action`1 visitMethod)
在System.Data.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel模型,IEnumerable`1命名空间)
在System.Data.Edm.Internal.EdmModelVisitor .VisitEdmModel(EdmModel item)
在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmModel(EdmModel项目)
在System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch()
在系统.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(键入en tityType)
在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(Action action,EntityState newState,Object entity,String methodName)
在System.Data.Entity.Internal.Linq.InternalSet`1.Add(对象实体)
在System.Data.Entity.DbSet`1.Add(TEntity实体)
在Nautix_EPOS.Controllers.HomeController.Index()在C:\sites\EPOS\Nautix EPOS \Nautix EPOS\Controllers\HomeController.cs:line 19
at lambda_method(Closure,ControllerBase,Object [])
在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object [ ]参数)
在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2参数)
在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,Ac System.Web.Mvc.ControllerActionInvoker中的
<> c__DisplayClass15。< InvokeActionMethodWithFilters> b__12()
在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter过滤器,ActionExecutingContext preContext,Func`1继续)


解决方案

可以重现你的问题。您的 MemberProduct 具有两个 Id 具有不同外壳的属性:

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

public int ID {get;组; }
}

EF代码首先在映射期间使用约定。惯例之一是将名为 Id 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 Framework添加项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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