-
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
Support C# 8.0 nullable reference types in model building #10347
Comments
@MarcoLoetscher This is absolutely something we want to leverage, but since we are generating additional semantics and behavior based on nullability we will need to consider carefully how to do this without making unintended breaking changes to model/schema generation. |
@ajcvickers @smitpatel do you want me to work on this? I'd first submit a design proposal for discussion (I'm assuming @smitpatel is pretty darn busy with other stuff). We can also discuss next week. |
Don't forget scaffolding - we should use C# 8 nullability to express nullability in generated classes. |
Notes from triage for scaffolding:
So probably just generate |
This is currently blocked by dotnet/roslyn#30143 |
One more important note... If you have a non-nullable auto-property, then you must have a constructor which initializes it, otherwise the compiler will warn/error. So we need to decide what we want to scaffold for non-nullable database columns, and also what we want our recommendations/code samples to look like. The options seem to be:
Any input? |
@roji The constructor feels like the cleanest. However, this makes me wonder how many people will actually want this level of strictness. That is, how many people will actually want to use non-nullable types if it means they need to start calling parameterized constructors (or deal with other code consequences) when they didn't before. I wonder if it should be the default for reverse engineering. |
At least in current preview of VS 2019, the default tfm for app is netcoreapp2.2 so nullable types are available but the default language version is still 7.0 (since 8.0 is in beta phase, which likely to change before RTM). So while VS shows that adding |
Yeah, it may be a good idea to reach out to the language/compiler people and confirm what they intend to do with this. @ajcvickers another thought... Our decision of how to scaffold may not be related to the user's choice at the project level - C# allows mixing null-aware and null-oblivious code quite easily. So even if the user hasn't opted into null awareness (because it seems too strict at the moment or whatever), we can still decide to generate a null-aware model. So I think it still makes sense to generate a nice, correct null-aware model with a full constructor - I can't really see much downside to it... |
Finally, we can always defer the scaffolding part of this for now, until the default language situation becomes more clear. |
Should models used on both EF6 and EF Core also be considered? Not sure if that is a common use case but I am currently in that situation (trying to slowly port a project from EF6 to EFCore) and the following workaround would not work on EF6 with lazy loading.
my current workaround is to enclose the navigational properties in
|
@gojanpaolo it would indeed be possible disable any EF Core behavior around new non-nullable references by simply disabling the nullable context around the relevant model properties, as you've done (or possibly the entire model). This would make EF Core behave exactly in the same way as today. Note that other means for specifying requiredness (i.e. the There are no plans to modify EF6 to recognize non-nullable references. |
As @ajcvickers mentioned in an offline discussion, this feature should make it into 3.0, because releasing it later would be a breaking change as non-nullable model properties will suddenly start getting treated as required by EF Core. For now we seem to agree to punt scaffolding non-nullable references. However, we still have to consider nullability within scaffolded models: we will probably need to disable the nullable context (by placing a |
Removing breaking-change label as this will only affect users opting into the NRT feature. |
Should this work for migrations already? Because I just tried to create a migration and it emitted |
@markushaslinger You might be hitting #16760, which is fixed in the nightly builds |
@ajcvickers thanks, will try the nightly |
Is there already some examples for this? I'm having an issue with initialising
|
@shoe-diamente I believe there's been some discussion about a better alternative, but, at least for now, you're best off just surrounding the properties with a Edit: Use |
@shoe-diamente a doc page with best practices for non-nullable types will be up soon. In the meantime, keep your DbSet properties non-nullable and initialize them to null with the null-forgiving operator (bang). See #17212 (comment) for more details. |
C# 8 is on the way and better to early than too late should be planned the implementation of the new features (Introducing Nullable Reference Types in C#).
A new logic is needed for mapping of e. g. string: Required and Optional Properties
Support for constructors with Parameters is required: Flexible mapping to CLR types and members (Custom O/C Mapping #240)
This list is probably not complete.
The text was updated successfully, but these errors were encountered: