none
Code First: "Sequence contains more than one matching element" Exception when generating schema

    Question

  • I have a simple model:

     

    public Class Item {
    
      public int Id { get; set;}
      public int Name { get; set; }
    
      public virtual Item ParentItem { get; set; }
      public virtual ICollection<Item> ChildrenItems { get; set; }
    }
    <br/>
    

     

    In the OnModelCreating, I'm trying to have the Item->ParentItem field be named ParentItemId rather than ItemId1. Here's the code I'm using for this:

     

    modelBuilder.Entity<Item>()
      .HasOptional(i => i.ParentItem)
      .WithMany()
      .IsIndependent()
      .Map(m => m.MapKey(e => e.Id, "ParentItemId"));
    

     

    If I comment out the modelBuilder mapping, it works perfectly.  When I execute with the modelBuilder mapping, I get the following exception:

     

    InvalidOperationException: Sequence contains more than one matching element]

       System.Lazy`1.get_Value() +9591079

       System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +371

       System.Data.Entity.Internal.InternalContext.Initialize() +16

       System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +15

       System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +62

       System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +15

       System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +40

       System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +315

       System.Linq.Enumerable.ToList(IEnumerable`1 source) +58

       Test.Controllers.ItemController.Index() in c:\src\Projects\Test\Controllers\ItemController.cs:21

       lambda_method(Closure , ControllerBase , Object[] ) +96

       System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17

       System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208

       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27

       System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55

       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263

       System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19

       System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191

       System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343

       System.Web.Mvc.Controller.ExecuteCore() +116

       System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97

       System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10

       System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37

       System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21

       System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12

       System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62

       System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50

       System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7

       System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22

       System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60

       System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9

       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8841105

       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

     

    Tuesday, January 11, 2011 4:31 PM

All replies

  • Hi Ken,

    Unfortunately this looks like a bug with mapping configuration for self-referencing IAs.

    One workaround is to create the association as an FK association by adding the FK property "ParentItemId" to your Item class. If you do that you won't need any explicit configuration of the association as ParentItemId will be automatically detected as the FK.

    We will fix the bug in the next release.

    Cheers,

    Andrew.

     

    Tuesday, January 11, 2011 6:23 PM