Skip to content

Analyzer warning for using required on DbSet properties #35937

Open
@roji

Description

@roji

In the following:

public class MyContext : DbContext
{
    public required DbSet<Blog> Blogs { get; set; }
}

It's common for users to use required, since the property is non-nullable and uninitialized: in normal circumstances, such a property triggers a warning for an uninitialized NRT property. However, EF comes with a diagnostics suppressor that removes the NRT warning, and the required just gets in the way: you can't simply initialize MyContext (you must also initialize Blogs), although the DbContext constructor takes care of injecting the property.

In other words, we have a pit of failure here... We should issue a warning for this case, with a code fix to remove the required.

/cc @DamianEdwards @davidfowl @AndriySvyryd, as discussed offline.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions