Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF core 3.1: should I initialize list navigation properties when using eager loading to load related entities?

Consider the following EF core 3.1 data model:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Post> Posts { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Author { get; set; }
    public DateTime Date { get; set; }
    public Blog Blog { get; set; }
    public int BlogId { get; set; }
}

public class BlogAppContext: DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=DB_BLOG_APP;Integrated Security=SSPI;MultipleActiveResultSets=True");
    }

    public DbSet<Post> Posts { get; set; }
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .HasMany(x => x.Posts)
            .WithOne(x => x.Blog)
            .HasForeignKey(x => x.BlogId)
            .IsRequired(); 
    }
}

Consider a scenario where I run the following query, by using eager loading:

static void Main(string[] args)
{
        using var context = new BlogAppContext();
        
        var blogs = context.Blogs.Include(x => x.Posts).ToList();

        foreach (var blog in blogs)
        {
            Console.WriteLine($"There are {blog.Posts.Count} posts");
        }
}

By looking at some examples, I've noticed that initalizing list navigation properties it's a common practice. In my case, this would lead to something like this:

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Post> Posts { get; set; } = new List<Post<();
}

I'm asking whether this is really useful when querying via eager loading.

I have done a few tests and I have verified that the query showed above automatically creates an empty list for a blog having no posts.

Put another way, it seems that even if the Posts navigation property is not initialized to an empty list inside the Blog entity definition, a query using the eager loading doesn't care and does not return a null value for the Posts navigation property.

Is my understanding correct ?

If it does, what is the usefullness (if any) of initializing the Posts navigation property to an empty list when querying the database by using eager loading to load related entities ?

like image 200
Enrico Massone Avatar asked Aug 30 '25 17:08

Enrico Massone


1 Answers

This is not useful and you don't have to do this because you are using eager loading(which is not very efficient if you load a lot of unnecessary data but this is another topic).

However, this is useful in two cases:

  • If you don't use eager loading because Posts property may be null
  • You are creating a new Blog object which is not yet saved in database and want to add a new Post into it. In this case Posts property must be initialized before Posts.Add(post) is called because null ref unitialized exception will be thrown.
like image 181
Deivydas Voroneckis Avatar answered Sep 02 '25 07:09

Deivydas Voroneckis