13

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

This shows the exceptions i'm getting.

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)
gunwin
  • 4,578
  • 5
  • 37
  • 59
  • Wow, if you didn't post the screenshot, I wouldn't have believed you – Mike Mooney Jan 13 '12 at 19:10
  • Can you post the code of `MemberProduct`? – nemesv Jan 13 '12 at 19:17
  • I've added the stack trace on. The database isn't actually created yet, as this is a code-first implementation of EF. Which is why I don't understand the 'more than one' element of the exception. It implies there is duplicate data or a primary key constraint being broken. – gunwin Jan 13 '12 at 19:17
  • have you overriden (?) the `GetHashCode` method of the MemberProduct class? what's the Key of that Entity? – sebagomez Jan 13 '12 at 19:30

4 Answers4

12

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.

nemesv
  • 138,284
  • 16
  • 416
  • 359
  • How do I use the key attribute? It's necessary to have to ID's as the ID property is inherited from an interface. – gunwin Jan 13 '12 at 19:31
  • You can find examples for Key e.g [here](http://stackoverflow.com/questions/5482670/entity-framework-code-first-define-the-key-for-this-entitytype). But I think you are misusing properties here: instead of the private properties use private fields: `private int id { get; set; }` change to `private int id;` etc. And It should also work. – nemesv Jan 13 '12 at 19:35
  • Or if you don't plan to put logic in the property accessors use plain [auto properies](http://msdn.microsoft.com/en-us/library/bb384054.aspx) and let the framework generate the private fields for you. – nemesv Jan 13 '12 at 19:40
  • This should be fixed in the next release. – Pawel Jan 26 '12 at 20:16
8

In case it is of any use to anybody:

I received this error when populating a model using EF from a stored procedure. The stored procedure returned multiple columns, two of which had the same alias/name.

B.Hawkins
  • 353
  • 5
  • 13
1

I suspect EF is getting confused by the two ID properties.

Change your private properties to fields; that should fix it.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

I got this sequence error when setting Entity state info incorrectly when initially seeding. Just make sure you're setting the state of object graphs correctly. My issue was with seeding data (not during the actual application run)...here's my answer anyway if it will help anyone if they're getting a sequence error (like "Sequence contains more than one matching element") when running the update-database command for migrations. While you're getting this error outside the seed method, I think your answer might lay in the Entity states for object graphs being set correctly.

Entity Framework Code First AddOrUpdate method insert Duplicate values

To get help with setting Entity object graphs correctly, see the following blog post which is the best I've found on correctly setting object state:

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

Community
  • 1
  • 1
firecape
  • 726
  • 6
  • 10