-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scaffolding and C# 8 nullable reference types #15520
Comments
I'll also add my two cents, that documentation for EF probably needs an update wrt to Nullable Reference Types. Right now, all tutorials and guides guide you to write your DbContext class as follows public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions options) : base(options) {}
public DbSet<MyEntity> MyEntities { get; set; }
} And this will produce a warning when compiling with NRTs. |
Triage: for 5.0, at least scaffold the disable. |
Why was this punted for version 5? It's painful when the entire project uses nullable, and then you have no idea with the model whether or not it's nullable. |
@grosch in 5.0, we scaffold Scaffolding nullable code was scoped out of EF 5.0 because of prioritization/time constraints, especially considering that EF 5.0 doesn't support other aspects of nullability (e.g. EF's API itself isn't annotated). We plan to address all that for EF 6.0. |
@roji I have done some experiment here, would be very grateful if you could sanity check the changes to this file (both for properties and Navigations): |
@ErikEJ did a quick review. |
Design decision:
|
#10347 makes EF Core recognize the new C# 8 nullable reference types (NRT), and treat them as
[Required]
. However, we still need to take care of scaffolding: the model we currently scaffold will not compile under projects with NRT enabled, as reference properties will be treated as non-nullable and the compiler will complain about them being uninitialized.According to @bricelam it should be relatively easy to detect whether the project we're scaffolding into has turned on NRT or not. This would allow us to scaffold differently based on that setting.
Nullable database columns should obviously be scaffolded as nullable properties:
For non-nullable columns, it seems that guidance is aligning to having non-nullable properties, forced-initialized to null as follows:
Note the null-forgiving bang operator which suppresses the warning. Alternatives for non-nullable properties include:
The text was updated successfully, but these errors were encountered: