title: Why is it that Entity Framework gives the error "Enumeration yielded no results" for typical many-to-many functionality?
body: <p>Our ASP.NET C# web application is used in the following environment .NET Framework 4</p>
  
  <p>ASP.NET Web Forms.</p>
  
  <p>IIS 7</p>
  
  <p>Windows 2008</p>
  
  <p>Visual Studio 2010</p>
  
  <p>.NET IDE C#</p>
  
  <p>HTTPS ( SSL )</p>
  
  <p>-Entity Framework 5</p>
  
  <p>In our application, we have the following tables.</p>
  
  <p>1) LearningCart</p>
  
  <p>2) EnrollmentEntriesInLearningCarts</p>
  
  <p>3) EnrollmentEntry</p>
  
  <p>4) Tutorial</p>
  
  <p>5) aspnet_Users</p>
  
  <p>There is a many-to-many relationship between the LearningCart and the EnrollmentEntry tables ( which have the EnrollmentEntriesInLearningCarts bridge table)</p>
  
  <p>The many-to-many relationship between the LearningCart and the EnrollmentEntry tables are depicted in the following Database diagram picture:</p>
  
  <p><img src="https://i.stack.imgur.com/Nqay3.png" alt="enter image description here"></p>
  
  <p>Also, I also ensured that the EnrollmentEntriesInLearningCarts bridge table already had data inside it:</p>
  
  <p><img src="https://i.stack.imgur.com/Os520.png" alt="enter image description here"></p>
  
  <pre><code> namespace PerlsData.Domain
   {
       using System;
       using System.Collections.Generic;
  
       public partial class LearningCart
       {
           public LearningCart()
           {
  
           }
  
           public virtual System.Guid LearningCartsDatabaseId { get; set; }
           public virtual short Type { get; set; }
           public virtual short Status { get; set; }
           public virtual string LearningCartsName { get; set; }
           public virtual Nullable&lt;System.Guid&gt; Creator { get; set; }
           public virtual Nullable&lt;System.Guid&gt; TeamLead { get; set; }
           public virtual Nullable&lt;short&gt; isSubmitted { get; set; }
           public virtual Nullable&lt;short&gt; isRemoved { get; set; }
           public virtual Nullable&lt;System.DateTime&gt; DateSharedWithInstructor { get; set; }
  
           public virtual ICollection&lt;EnrollmentEntry&gt; associatedEnrollmentEntry { get; set; }     
  
       }
   }
   namespace PerlsData.Domain
   {
       using System;
       using System.Collections.Generic;
  
       public partial class EnrollmentEntry
       {
           public EnrollmentEntry()
           {
  
           }
  
           public virtual System.Guid EnrollmentEntryDatabaseID { get; set; }
           public virtual System.Guid UserId { get; set; }
           public virtual System.Guid TutorialDatabaseID { get; set; }
  
           public virtual aspnet_Users aspnet_Users { get; set; }
           public virtual Tutorial Tutorial { get; set; }
  
           public virtual ICollection&lt;LearningCart&gt; associatedLearningCart { get; set; }
       }
         }
  
  
    namespace PerlsData.Domain
    {
        using System;
        using System.Collections.Generic;
  
        public partial class aspnet_Users
        {
           public aspnet_Users()
           {
  
           }
  
           public System.Guid ApplicationId { get; set; }
           public System.Guid UserId { get; set; }
           public string UserName { get; set; }
           public string LoweredUserName { get; set; }
           public string MobileAlias { get; set; }
           public bool IsAnonymous { get; set; }
           public System.DateTime LastActivityDate { get; set; }
  
        }  
   }
  
   namespace PerlsData.Domain
   {
       using System;
       using System.Collections.Generic;
  
       public partial class Tutorial
       {
           public Tutorial()
           {
  
       }
  
           public virtual System.Guid TutorialDatabaseID { get; set; }
           public virtual string TutorialCode { get; set; }
           public virtual System.Guid SectionDatabaseID { get; set; }
           public virtual System.Guid UserId { get; set; }
  
           public virtual aspnet_Users aspnet_Users { get; set; }
           public virtual ICollection&lt;EnrollmentEntry&gt; EnrollmentEntries { get; set; }
  
           public virtual Section Section { get; set; }
  
       }
   }
  
    namespace PerlsData
    {
       public class Context : System.Data.Entity.DbContext
       {
  
           public DbSet&lt;PerlsData.Domain.EnrollmentEntry&gt; EnrollmentEntries { get; set; }
           public DbSet&lt;PerlsData.Domain.LearningCart&gt;  LearningCarts { get; set; }
  
           public Context()
                : base("Name=LocalSqlServer")
           {
  
           }
  
           protected override void OnModelCreating(DbModelBuilder modelBuilder)
           {
  
               base.OnModelCreating(modelBuilder);
  
               modelBuilder.Entity&lt;PerlsData.Domain.LearningCart&gt;().ToTable("LearningCarts",      schemaName: "dbo");
  
               modelBuilder.Entity&lt;PerlsData.Domain.LearningCart&gt;().HasKey(t =&gt;      t.LearningCartsDatabaseId);
  
               modelBuilder.Entity&lt;PerlsData.Domain.LearningCart&gt;().Property(t =&gt;           t.LearningCartsDatabaseId)
                                                                                                .HasColumnName("LearningCartsDatabaseId")
                                                                                           .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
                    modelBuilder.Entity&lt;PerlsData.Domain.LearningCart&gt;().HasMany(learnC      =&gt;           learnC.associatedEnrollmentEntry)
                        .WithMany(enEnt =&gt; enEnt.associatedLearningCart)
                         .Map(
                              m =&gt;
                              {
                                  m.ToTable("EnrollmentEntriesInLearningCarts", schemaName:      "dbo");
                                  m.MapLeftKey("LearningCartsDatabaseId");
                                  m.MapRightKey("EnrollmentEntryDatabaseID");
  
                              }
                     );
  
                    modelBuilder.Entity&lt;PerlsData.Domain.EnrollmentEntry&gt;     ().ToTable("EnrollmentEntry", schemaName: "dbo");
  
               modelBuilder.Entity&lt;PerlsData.Domain.EnrollmentEntry&gt;().HasKey(e =&gt;      e.EnrollmentEntryDatabaseID);
  
  
               modelBuilder.Entity&lt;PerlsData.Domain.EnrollmentEntry&gt;().Property(t =&gt;      t.EnrollmentEntryDatabaseID)
                   .HasColumnName("EnrollmentEntryDatabaseID")
                   .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
               modelBuilder.Entity&lt;PerlsData.Domain.EnrollmentEntry&gt;().Property(t =&gt;      t.TutorialDatabaseID)
                                                                                        .HasColumnName("TutorialDatabaseID");
  
               modelBuilder.Entity&lt;PerlsData.Domain.EnrollmentEntry&gt;().HasMany(enEnt =&gt;      enEnt.associatedLearningCart)
                   .WithMany(learnC =&gt; learnC.associatedEnrollmentEntry)
                   .Map(
                         m =&gt;
                         {
                             m.ToTable("EnrollmentEntriesInLearningCarts", schemaName: "dbo");
                             m.MapLeftKey("EnrollmentEntryDatabaseID");
                             m.MapRightKey("LearningCartsDatabaseId");
  
                         }
                );
  
               modelBuilder.Entity&lt;PerlsData.Domain.aspnet_Users&gt;().ToTable("aspnet_Users",      schemaName: "dbo");
  
               modelBuilder.Entity&lt;PerlsData.Domain.aspnet_Users&gt;()
               .Property(au =&gt; au.UserId)
               .HasColumnName("UserId");
  
               modelBuilder.Entity&lt;PerlsData.Domain.aspnet_Users&gt;()
               .HasKey(au =&gt; au.UserId);
  
               modelBuilder.Entity&lt;PerlsData.Domain.aspnet_Users&gt;()
               .Property(au =&gt; au.UserName)
               .HasColumnName("UserName");
  
               modelBuilder.Entity&lt;PerlsData.Domain.Tutorial&gt;().ToTable("Tutorial",      schemaName: "dbo");
  
               modelBuilder.Entity&lt;PerlsData.Domain.Tutorial&gt;().Property(t =&gt;      t.TutorialDatabaseID)
       .HasColumnName("TutorialDatabaseID")
       .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
  
          modelBuilder.Entity&lt;PerlsData.Domain.Tutorial&gt;()..HasKey(t =&gt;      t.TutorialDatabaseID);
  
               modelBuilder.Entity&lt;PerlsData.Domain.Tutorial&gt;().HasRequired(x =&gt; x.Section)
                   .WithMany(x =&gt; x.Tutorials)
                   .HasForeignKey(x =&gt; x.SectionDatabaseID);
  
           }
       } 
  
  
   }
  </code></pre>
  
  <p>Here's is where the code fails to retrieve information:</p>
  
  <pre><code>         public LearningCart       getLearningCartAssociatedWithLoggedInStudentsTutorial(Guid userOfInterestGuid, Guid      tutorialOfInterestGuid){
  
                   LearningCart learningCartsAssociatedWithLoggedInStudentTutorial = new LearningCart();
                   EnrollmentEntry enrollmentEntryAssociatedWithLoggedInStudentTutorial = new EnrollmentEntry();
  
                   LearningCart learnCartOfInterest = null;
                   IQueryable&lt;LearningCart&gt; learnCartIQueryable = null;
                   EnrollmentEntry enrEntOfInterest = null;
  
                   using (PerlsData.Context context = new PerlsData.Context())
                   {
  
                       context.Configuration.LazyLoadingEnabled = true;
  
                       context.Configuration.ProxyCreationEnabled = true;
  
                        IQueryable&lt;EnrollmentEntry&gt; enrollmentEntryOfInterestIQueryable =
                                                                                context.EnrollmentEntries.Where(ee =&gt;     
                                                                             ee.aspnet_Users.UserId == userOfInterestGuid
                                                                        &amp;&amp;           ee.Tutorial.TutorialDatabaseID == tutorialOfInterestGuid);
  
                        foreach (EnrollmentEntry enrEnt in enrollmentEntryOfInterestIQueryable)
                       {
  
  
                           enrEntOfInterest = enrEnt;
  
  
                       }
  
                            learnCartIQueryable = context.LearningCarts.Where(lc =&gt;
                                                                       lc.associatedEnr ollmentEntry.Any(ee =&gt;
                                                                              ee.EnrollmentEntryDatabaseID == enrEntOfInterest.EnrollmentEntryDatabaseID));
  
                           foreach (LearningCart lc in learnCartIQueryable)
                           {
                                learnCartOfInterest = lc;
                           }
  
                       context.Entry(learnCartOfInterest).State =      System.Data.EntityState.Detached;
  
                   }
  
  
                   return learnCartOfInterest;
               }
  </code></pre>
  
  <p>The problem comes about with trying to figure out a specific LearningCart module.
  I try to retrieve a LearningCart, and place it in the IQueryable object:</p>
  
  <pre><code> IQueryable&lt;LearningCart&gt; learnCartIQueryable = context.LearningCarts.Where(lc =&gt;
                                                                       lc.associatedEnrollmentEntry.Any(ee =&gt;
                                                                         ee.EnrollmentEntryDatabaseID == enrEntOfInterest.EnrollmentEntryDatabaseID));
  </code></pre>
  
  <p>Using Visual Studio 2010, I put learnCartIQueryable under "Add Watch", and I got "Enumeration yielded no results"</p>
  
  <p>I already do have data inside in the EnrollmentEntriesInLearningCarts bridge table.</p>
  
  <p>Why is it that Entity Framework fails to retrieve the data in the EnrollmentEntriesInLearningCarts bridge table, and only gives the error "Enumeration yielded no results" ?</p>