Entity Framework Core (EF 7) many-to-many results always null

entity-framework-core

Question

I have followed the instructions for the workaround for many-to-many described in Issue #1368 and the Docs Here... but when I try to navigate, it always returns null.

My Models:

    public class Organization
{
    public Guid OrganizationID { get; set; }

    //...

    public ICollection<OrganizationSubscriptionPlan> OrganizationSubscriptionPlans { get; set; }

}

public class SubscriptionPlan
{
    public int SubscriptionPlanID { get; set; }

    //...

    public ICollection<OrganizationSubscriptionPlan> OrganizationSubscriptionPlans { get; set; }


public class OrganizationSubscriptionPlan
{
    [ForeignKey("Organization")]
    public Guid OrganizationID { get; set; }
    public Organization Organization { get; set; }

    [ForeignKey("SubscriptionPlan")]
    public int SubscriptionPlanID { get; set; }
    public SubscriptionPlan SubscriptionPlan { get; set; }
}

ApplicationDbContext:

    protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    builder.Entity<OrganizationSubscriptionPlan>().HasKey(x => new { x.OrganizationID, x.SubscriptionPlanID });
    builder.Entity<OrganizationSubscriptionPlan>().HasOne(x => x.Organization).WithMany(x => x.OrganizationSubscriptionPlans).HasForeignKey(x => x.OrganizationID);
    builder.Entity<OrganizationSubscriptionPlan>().HasOne(x => x.SubscriptionPlan).WithMany(x => x.OrganizationSubscriptionPlans).HasForeignKey(x => x.SubscriptionPlanID);
}

And my Query:

var organizations = _context.Organizations
    .Include(o => o.OrganizationSubscriptionPlans);

foreach (var organization in organizations)
{
    //....
    var subscriptions = organization.OrganizationSubscriptionPlans
            .Select(s => s.SubscriptionPlan);
     // ^^^^^^^^^^^ why is subscriptions always null?
}

The "organizations" query returns the results as expected, including the list of OrganizationSubscriptionPlans within each one, but when I try to navigate to them in the foreach loop the "subscriptions" query returns null every time. What am I doing wrong?

1
2
6/24/2016 6:20:38 PM

Accepted Answer

Turns out it's a Lazy Loading issue. You have to "Include" the joining entity and then "ThenInclude" the other entity.

var organizations = _context.Organizations
    .Include(o => o.OrganizationSubscriptionPlans)
    .ThenInclude(s => s.SubscriptionPlan);
5
6/27/2016 2:55:47 PM

Popular Answer

ForeignKey attr is to decorate reference properties to indicate them what primitive property hold the FK value.

public class OrganizationSubscriptionPlan
{    
    public Guid OrganizationID { get; set; }
    [ForeignKey("OrganizationID")]
    public Organization Organization { get; set; }

    public int SubscriptionPlanID { get; set; }
    [ForeignKey("SubscriptionPlanID")]
    public SubscriptionPlan SubscriptionPlan { get; set; }
}


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