2

My first question - be kind :-).

In the code below, I am attempting to reference an "Include(d)" entity (Schedules) to obtain its Name property. In EF 6, both "schedule" and "schedule_2" return the correct value of Name. In EF Core, "schedule" returns NULL and "schedule_2" returns the correct value of Name.

I do not understand why I should have to load the "schedules" List. Shouldn't the .Include force an Eager Load of the Schedules for each Election such that each Election Schedule's Name property would be available for the "schedule" assignment?

// Relevant Model entities in database
//         DbSet<Election> Elections { get; set; }
//
// The following are the related classes defined in the database context...
public class Election
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Schedule> Schedules { get; set; }
}
public class Schedule
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public int? CfsElectionId { get; set; }
    public string Name { get; set; }

    [Required] // sets cascade delete
    [ForeignKey("CFSElectionID")]
    public virtual Election Election { get; set; }
}

class Program
{
    static void Main()
    {
        var db = new FfmsDbContext();
        var elections = db.Elections
            .Include(i => i.Schedules)
            .ToList();

        //The following returns NULL?
        var schedule = elections.First().Schedules?.First().Name ?? "NULL";

        var schedules = db.Schedules
            .ToList();

        //The following returns the correct Name property?
        var schedule_2 = elections.First().Schedules?.First().Name ?? "NULL";

        Console.WriteLine($@"sched: {schedule}");
        Console.WriteLine($@"schedules.First().Name: {schedules.First().Name}");
        Console.WriteLine($@"sched2: {schedule_2}");

        Console.WriteLine("Done...");
        Console.ReadLine();
    }
}

/*
  Output...
    sched: NULL
    schedules.First().Name: Candidates
    sched2: Candidates
    Done...
*/
2
  • have you tried configure this.Configuration.LazyLoadingEnabled = true; ? in DBContext ? Commented Jan 30, 2022 at 3:06
  • @zey -- Thanks for the idea. I've now tried all possible uses of LazyLoadingEnabled. In my reading though, I don't see how this parameter helps though. What I want is Eager loading (always load the related entity on first access). Commented Jan 30, 2022 at 5:15

1 Answer 1

11

Turns out that my problem ended up being in the References of the Class.

I had accidentally chosen System.Data.Entity as the offered choice for .Include.

The correct reference should have been Microsoft.EntityFrameworkCore.

Once I adjusted the reference, the .Include worked as desired.

Sign up to request clarification or add additional context in comments.

5 Comments

Eugh also did this! Thanks so much for posting - I could have wasted a lot (more) time looking for this!
I can't believe this doesn't throw an error somewhere, like "Invalid use of Include on System.Data.Entity" I wasted a lot of time before I found this!
Thanks a lot! I wasted a few hours in this trap when upgrading a project from .NET Framework 4.8 with EF6 to .NET 6 with EF Core 6. Interestingly, it still works with the In-Memory provider (maybe because it always does eager loading), but not with a real SQL Server database.
god bless you. I spent 2 days trying to figure out why the same db context behaves differently for 2 different classes. So stupid :)
@Fred Schmidt Thanks for your sharing. Otherwise, I don't know how much more time I will continue wasting on this silly error. It should be improved to make it error out on compiling time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.