Our ASP.NET C# web application is used in the following environment .NET Framework 4
ASP.NET Web Forms.
IIS 7
Windows 2008
Visual Studio 2010
.NET IDE C#
HTTPS ( SSL )
-Entity Framework 5
In our application, we have the following tables.
1) LearningCart
2) EnrollmentEntriesInLearningCarts
3) EnrollmentEntry
4) Tutorial
5) aspnet_Users
There is a many-to-many relationship between the LearningCart and the EnrollmentEntry tables ( which have the EnrollmentEntriesInLearningCarts bridge table)
The many-to-many relationship between the LearningCart and the EnrollmentEntry tables are depicted in the following Database diagram picture:
Also, I also ensured that the EnrollmentEntriesInLearningCarts bridge table already had data inside it:
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<System.Guid> Creator { get; set; }
public virtual Nullable<System.Guid> TeamLead { get; set; }
public virtual Nullable<short> isSubmitted { get; set; }
public virtual Nullable<short> isRemoved { get; set; }
public virtual Nullable<System.DateTime> DateSharedWithInstructor { get; set; }
public virtual ICollection<EnrollmentEntry> 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<LearningCart> 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<EnrollmentEntry> EnrollmentEntries { get; set; }
public virtual Section Section { get; set; }
}
}
namespace PerlsData
{
public class Context : System.Data.Entity.DbContext
{
public DbSet<PerlsData.Domain.EnrollmentEntry> EnrollmentEntries { get; set; }
public DbSet<PerlsData.Domain.LearningCart> LearningCarts { get; set; }
public Context()
: base("Name=LocalSqlServer")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PerlsData.Domain.LearningCart>().ToTable("LearningCarts", schemaName: "dbo");
modelBuilder.Entity<PerlsData.Domain.LearningCart>().HasKey(t => t.LearningCartsDatabaseId);
modelBuilder.Entity<PerlsData.Domain.LearningCart>().Property(t => t.LearningCartsDatabaseId)
.HasColumnName("LearningCartsDatabaseId")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<PerlsData.Domain.LearningCart>().HasMany(learnC => learnC.associatedEnrollmentEntry)
.WithMany(enEnt => enEnt.associatedLearningCart)
.Map(
m =>
{
m.ToTable("EnrollmentEntriesInLearningCarts", schemaName: "dbo");
m.MapLeftKey("LearningCartsDatabaseId");
m.MapRightKey("EnrollmentEntryDatabaseID");
}
);
modelBuilder.Entity<PerlsData.Domain.EnrollmentEntry> ().ToTable("EnrollmentEntry", schemaName: "dbo");
modelBuilder.Entity<PerlsData.Domain.EnrollmentEntry>().HasKey(e => e.EnrollmentEntryDatabaseID);
modelBuilder.Entity<PerlsData.Domain.EnrollmentEntry>().Property(t => t.EnrollmentEntryDatabaseID)
.HasColumnName("EnrollmentEntryDatabaseID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<PerlsData.Domain.EnrollmentEntry>().Property(t => t.TutorialDatabaseID)
.HasColumnName("TutorialDatabaseID");
modelBuilder.Entity<PerlsData.Domain.EnrollmentEntry>().HasMany(enEnt => enEnt.associatedLearningCart)
.WithMany(learnC => learnC.associatedEnrollmentEntry)
.Map(
m =>
{
m.ToTable("EnrollmentEntriesInLearningCarts", schemaName: "dbo");
m.MapLeftKey("EnrollmentEntryDatabaseID");
m.MapRightKey("LearningCartsDatabaseId");
}
);
modelBuilder.Entity<PerlsData.Domain.aspnet_Users>().ToTable("aspnet_Users", schemaName: "dbo");
modelBuilder.Entity<PerlsData.Domain.aspnet_Users>()
.Property(au => au.UserId)
.HasColumnName("UserId");
modelBuilder.Entity<PerlsData.Domain.aspnet_Users>()
.HasKey(au => au.UserId);
modelBuilder.Entity<PerlsData.Domain.aspnet_Users>()
.Property(au => au.UserName)
.HasColumnName("UserName");
modelBuilder.Entity<PerlsData.Domain.Tutorial>().ToTable("Tutorial", schemaName: "dbo");
modelBuilder.Entity<PerlsData.Domain.Tutorial>().Property(t => t.TutorialDatabaseID)
.HasColumnName("TutorialDatabaseID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<PerlsData.Domain.Tutorial>()..HasKey(t => t.TutorialDatabaseID);
modelBuilder.Entity<PerlsData.Domain.Tutorial>().HasRequired(x => x.Section)
.WithMany(x => x.Tutorials)
.HasForeignKey(x => x.SectionDatabaseID);
}
}
}
Here's is where the code fails to retrieve information:
public LearningCart getLearningCartAssociatedWithLoggedInStudentsTutorial(Guid userOfInterestGuid, Guid tutorialOfInterestGuid){
LearningCart learningCartsAssociatedWithLoggedInStudentTutorial = new LearningCart();
EnrollmentEntry enrollmentEntryAssociatedWithLoggedInStudentTutorial = new EnrollmentEntry();
LearningCart learnCartOfInterest = null;
IQueryable<LearningCart> learnCartIQueryable = null;
EnrollmentEntry enrEntOfInterest = null;
using (PerlsData.Context context = new PerlsData.Context())
{
context.Configuration.LazyLoadingEnabled = true;
context.Configuration.ProxyCreationEnabled = true;
IQueryable<EnrollmentEntry> enrollmentEntryOfInterestIQueryable =
context.EnrollmentEntries.Where(ee =>
ee.aspnet_Users.UserId == userOfInterestGuid
&& ee.Tutorial.TutorialDatabaseID == tutorialOfInterestGuid);
foreach (EnrollmentEntry enrEnt in enrollmentEntryOfInterestIQueryable)
{
enrEntOfInterest = enrEnt;
}
learnCartIQueryable = context.LearningCarts.Where(lc =>
lc.associatedEnr ollmentEntry.Any(ee =>
ee.EnrollmentEntryDatabaseID == enrEntOfInterest.EnrollmentEntryDatabaseID));
foreach (LearningCart lc in learnCartIQueryable)
{
learnCartOfInterest = lc;
}
context.Entry(learnCartOfInterest).State = System.Data.EntityState.Detached;
}
return learnCartOfInterest;
}
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:
IQueryable<LearningCart> learnCartIQueryable = context.LearningCarts.Where(lc =>
lc.associatedEnrollmentEntry.Any(ee =>
ee.EnrollmentEntryDatabaseID == enrEntOfInterest.EnrollmentEntryDatabaseID));
Using Visual Studio 2010, I put learnCartIQueryable under "Add Watch", and I got "Enumeration yielded no results"
I already do have data inside in the EnrollmentEntriesInLearningCarts bridge table.
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" ?