dotnet / dotnet/efcore

Analyzer warning for using required on DbSet properties

Open
#35,937 1 comment 0 reactions 0 assignees View on GitHub
area-analyzer
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

In the following:

```c#
public class MyContext : DbContext
{
public required DbSet 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.