EF 6 - One To Many Mapping Always Null

asp.net-mvc c# entity-framework one-to-many

Question

I can't seem to resolve my mapping issue; The relationship is one user may have many venues, A venue must have a user.

My venue class looks like:

public class Venue : BaseObject, IBaseObject
{
    [Required]
    public virtual User Owner { get; set; }

    [Required]
    [MaxLength(50)]
    public string Name { get; set; }
}

My User class looks like:

public class User : BaseObject, IBaseObject
{

    [Required]
    public string Name { get; set; }

    [Required]
    [DisplayName("Email")]
    public string EmailAddress { get; set; }

    [Required]
    public string Password { get; set; }

    public bool Active { get; set; }


    public virtual ICollection<Venue> Venues { get; set; } 
}

DbContextClass as requested

 public class SystemContext : DbContext, IDbContext
{
    public SystemContext() :
        base("SystemContext")
    {
        Database.SetInitializer<SystemContext>(null);
        Configuration.ProxyCreationEnabled = false;
        Configuration.LazyLoadingEnabled = true;
    }


    public SystemContext(string connectionstringname = "SystemContext") :
        base(connectionstringname)
    {
        Database.SetInitializer<SystemContext>(null);
        Configuration.ProxyCreationEnabled = false;


    }
    public new IDbSet<T> Set<T>() where T : class
    {
        return base.Set<T>();
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<User>().HasMany(x=>x.Venues);
    }



    public DbSet<PublicQueries> PublicQueries { get; set; }
    public DbSet<Venue> Venues { get; set; }
    public DbSet<User> Users { get; set; }
}

When I load the User class from the database Venues always seems to be null?

I've done something similar before but can't remember how I resoled this.

Thanks

1
7
2/23/2014 11:01:36 PM

Popular Answer

Pretty sure this is a lazy-load issue. If you load an object with navigation properties such as your ICollection<Venues> then they won't be included by default, because they might have more navigation properties linking to more objects, which may have more navigation properties... and before you know it you're loading half the database. This is a particular problem when you've disposed of the context the object came out of by the time you go to access that navigation property, because then it doesn't have a database connection to load them from even if it did realise that it should be doing so.

The fix is to tell Entity Framework that you want that property to be populated, by adding .Include(u => u.Venues); when you get them from the DbSet. You'll need to include System.Data.Entity to get that particular overload of Include().

12
2/23/2014 3:47:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow