Josh Bula
Josh Bula

Reputation: 413

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

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?

Upvotes: 3

Views: 1697

Answers (2)

Josh Bula
Josh Bula

Reputation: 413

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);

Upvotes: 5

E-Bat
E-Bat

Reputation: 4892

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; }
}

Upvotes: 0

Related Questions