How can I do a [MemberNotNull] for an Entity Framework IncludeAll() method I wrote?

David Thielen 3,121 Reputation points
2023-09-16T17:40:21.58+00:00

I have the following helper method. This includes all complex properties in my Event DbSet (actually there's 9 properties but I reduced it for clarity):

public static IQueryable<Event> IncludeAll(this IQueryable<Event> source)
{
    return source
        .Include(e => e.Description)
        .Include(e => e.Tags)
        .Include(e => e.Thumbnail);
}

I use it as follows:

_event = await dbContext.Events
    .IncludeAll()
    .FirstOrDefaultAsync(e => e.Id == pkEvent);

When I do this, I get warnings in that call that it may need Description, Tags, & Thumbnail. And in the first use of each, again warnings that they may be null.

Is there a way to do something like the following:

[MemberNotNull(nameof(Picture))]
public static IQueryable<Event> IncludeAll(this IQueryable<Event> source)

That (declaring MemberNotNull) does not work.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
741 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,924 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 65,211 Reputation points
    2023-09-16T19:17:38.38+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.