Entity Framework 6 "HasRequired" and "WithMany" partially working - not working as expected

.net c# entity-framework entity-framework-6


I finally forced myself to ask here after searching endlessly for three hours and driving myself insane. I've used Entity Framework for more than 5 years, and during that time, I've never seen the issue I explain below.

I'm enrolled in two POCO classes.

public class Company
    public virtual ICollection<BusinessUnit> BusinessUnits { get; protected set; } = new List<BusinessUnit>();

    public int Id { get; protected set; }

public class BusinessUnit
    public virtual Company Company { get; protected set; } = new Company();

    public int CompanyId { get; protected set; }

    public string Description { get; protected set; }

And I state the following in my "BusinessUnit" configuration:

            .HasRequired(c => c.Company)
            .WithMany(c => c.BusinessUnits)
            .HasForeignKey(c => c.CompanyId);

What follows functions:

  • Run separate DbSet queries on the 2 entities.
  • navigating to the "Company" navigation property on my obtained "BusinessUnit" object in N to 1

The following is ineffective:

  • Even when using "Include" to perform eager loading, the 1 to N "BusinessUnits" navigation property on the recovered "Company" entity is always NULL.

What I am now doing is accurate based on EF instructions and my personal experience:

  • The lazy loading feature is active (the 1 navigation works indeed)
  • proxies are operational (same as above)
  • Due to the ICollection's virtual nature, sluggish loading is permitted.
  • I can access the "Company" proxy on the "BusinessUnit" entity thanks to the "HasRequired" component of the mapping working.

Although I tried switching the property setters to public, the issue has not been resolved.

This is acting strangely for no apparent reason. Clearly, I'm missing something, but I'm at a loss as to what.

The version of Entitfy Framerowk running on.Net 4.6 is 6.1.3, and it was installed via a NuGet package.

12/29/2015 1:20:23 PM

Accepted Answer

12/29/2015 1:24:47 PM

Related Questions


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