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
Include
method? – Marseillaisethis.Venues = new Collection<Venue>();
– Postfix