-
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
Update handling of non-nullable store-generated properties #15070
Comments
Note for triage: related to #13613 |
Note for triage: should our guidance here be to do this: public partial class Person
{
public State State { get; set; } = State.Sleeping;
} This is basically what we decided to scaffold in #13613--although we don't reverse engineer to enums by default its still the same pattern. This works because a value will always be inserted, but if no value was explicitly set, then The property above should not have a call to modelBuilder.Entity<Person>(entity =>
{
entity.Property(e => e.State)
.ValueGeneratedNever()
.HasDefaultValue(State.Sleeping);
}); Another option is to break to Let's discuss. (Again. 🚎) |
Notes for the design meetingCurrent behaviorCalling
Making something a store-generated property means:
This mechanism works well except when the CLR default is a valid value that needs to be inserted. For example, if the default for a bool column should be true, then setting the CLR default (false) will result in using the default of true, and so there is no way to end up with a value of false in the database. In addition, the call to OptionsPatterns that make this better, using as an example a bool with default true. A: Use client-side defaultsWe would scaffold: public class Blog
{
public bool IsValid { get; set; } = true;
} and .HasDefaultValue(true)
.ValueGeneratedNever(); (If we introduce a scaffolding mode which does not include all database artifacts, then we can skip this.) When writing code manually, we would recommend just: public class Blog
{
public bool IsValid { get; set; } = true;
} and no store-generated config unless you need Migrations to create the default in the database. B: Use regular properties backed by nullable fieldThis approach would preserve all store-generated behavior at the expense of more code. Also, we need to make changes to EF to make this work, but they should not be complex. Code is the same for both scaffolding and writing manually: public class Blog
{
private bool? _isValid;
public bool IsValid
{
get => _isValid ?? false;
set => _isValid = value;
}
} and .HasDefaultValue(true); (There should be no need to set the property access mode since we read/write fields by default in 3.0.) C: Use nullable propertiesThis preserves store generated behavior, but forces the type in the model to be nullable. public class Blog
{
public bool? IsValid { get; set; };
} and .HasDefaultValue(true); ProposalFor reverse engineering:
To document/recommend for "Code First":
|
Notes from design meeting:
|
…as not been set Fixes #701 Part of #13224, #15070, #13613 This PR contains the underlying model and change tracking changes needed to support sentinel values. Future PRs will add model building API surface and setting the sentinel automatically based on the database default. There is a breaking change here: explicitly setting the value of a property with a temporary value no longer automatically makes the value non temporary.
…as not been set Fixes #701 Part of #13224, #15070, #13613 This PR contains the underlying model and change tracking changes needed to support sentinel values. Future PRs will add model building API surface and setting the sentinel automatically based on the database default. There is a breaking change here: explicitly setting the value of a property with a temporary value no longer automatically makes the value non temporary.
Fixes #15070 - Warn for enums like we do for bools - Don't warn if a sentinel value has been configured - Update warning message to mention sentinel value - Set the sentinel by convention for non-nullable bools with a default value
Fixes #15070 - Warn for enums like we do for bools - Don't warn if a sentinel value has been configured - Update warning message to mention sentinel value - Set the sentinel by convention for non-nullable bools with a default value
If you make the default value of an enum that is mapped to the database anything other then 0 it always defaults every value saved to the default value.
Example with code: If I do default sleeping and try to set it to active and save. It will change it back to sleeping in the savechanges. But if you leave the default at 0 aka active then you can change and save as normal.
EfcoreEnumTester.zip
The text was updated successfully, but these errors were encountered: