I'm having difficulties acchieving certain functionality when using Entity Framework's Code First approach. Using the Fluent API I was trying to accomplish that the Agencies property of a User loads all Agency entities whose Principal or Agent contain him as a user.
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public IList<Agency> Agencies { get; set; }
}
public class Agency
{
public int Id { get; set; }
[Required]
public User Principal { get; set; }
[Required]
public User Agent { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().HasMany(u => u.Agencies).WithRequired(a => a.Agent).WillCascadeOnDelete(true);
modelBuilder.Entity<User>().HasMany(u => u.Agencies).WithRequired(a => a.Principal).WillCascadeOnDelete(true);
}
For some reason only the Agencies whose Principal is set to the owning User are being loaded. Any ideas why or how I could alternatively achieve the functionality I'm asking?