-
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
Entity Framework SQLite Treats 0 As NULL For Non-Nullable fields #30620
Comments
Can you please submit a minimal, runnable repro? The above is notably missing your model (how is HouseNumber configured) and the data. |
@roji I am not sure what you mean. HouseNumber is configured in Address class using attributes |
@s8moahme @bricelam I know we have discussed this before, but shouldn't we warn for this kind of schema? |
Note from triage: attempt to warn in this case that there is no mechanism in the database to generate the default value. |
I came across this problem today on a context scaffolded from a SQLite database using
To my surprise,
Now, if in my application I try to create a new entity instance, assign 0 to e.DirectionVectorX, and attempt to insert this, EF appears to either set this column to null or omit it (I haven't checked the actual SQL). Regardless, the result is a Removing the HasDefaultValueSql() statements from the context fixes the problem and I can assign values freely. |
I am facing a problem with Entity Framework SqLite where 0 is interpreted as NULL. Here is the code to reproduce the issue:
There are 2 problems with this code. First, HouseNumber is not generated by the database. CoreTestContext.GenerateData method throws the following exception:
SqliteException: SQLite Error 19: 'NOT NULL constraint failed: Addresses.HouseNumber'.
If HouseNumber is set to a non-zero value as in the commented out line, the SqliteException does not get thrown and Assert.Single fails with the following exception:
Xunit.Sdk.SingleException: 'The collection was expected to contain a single element, but it was empty.'
Here is what I found after some investigation:
When HouseNumber is set to 0, the same SqliteException is thrown
As mentioned above, when HouseNumber is set to a non-zero value, the SqliteException does not get thrown
When Status.Error value is set to a non-zero value, the assertion succeeds
When Status.Error value is set to 0, address.Status value is changed to Status.None once MasterDbDataContext.SaveChanges is executed in CoreTestContext.GenerateData
When Address.Status is changed to nullable, it does not change after MasterDbDataContext.SaveChanges is executed
This tells me that entity framework treats 0 as NULL (or unassigned value) when the field is non-nullable.
For example, address.Status value is set to 0 (Status.Error), which is treated as if the value was not assigned and therefore, the value is overwritten to Status.None as expected from the default value specified in the Address class.
Also, Address.HouseNumber is seen as NULL when it is assigned a 0 value as seen in the SqliteException
Can someone please explain to me why entity framework is behaving that way with SQLite? My understanding is that SQLite differentiates between 0 and NULL, but this is clearly not happening here.
The text was updated successfully, but these errors were encountered: