Skip to content

Exclude CS8618 for DbSet<T> in a derived DbContext class #45912

@Youssef1313

Description

@Youssef1313

See this article from EF Core docs.

When nullable reference types are enabled, the C# compiler emits warnings for any uninitialized non-nullable property, as these would contain null. As a result, the common practice of defining a non-nullable DbSet on a context will now generate a warning. However, EF Core always initializes all DbSet properties on DbContext-derived types, so they are guaranteed to never be null, even if the compiler is unaware of this.

There is a workaround mentioned in the docs to add = null!, but I feel it's reasonable to exclude any public DbSet that exists in a class derived from DbContext by default.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions