-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core #25359
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Duplicate of #23564 |
In the issue that you are refering to the second rule of retrieving optional owned entity states: |
Refer the first rule
Both the properties - From migrations perspective, I am not sure if this should be allowed. Effectively adding a required property in owned type deletes the owned entity. cc: @bricelam |
So the issue is that the new required columns contain NULL? I wonder if something #2595 would improve the Migrations experience by ensuring that a value is provided for already-existing owned entities. |
Yes, owned type is adding a required property and we generate migration which puts null values for existing columns which should be non-null. |
Thank you for the answer. I am not sure if I am missing something, but do I understand correctly that by convention, if the property type is non-nullable (in value types), the generated migration should set the columns as non-nullable as well? Or is it that if the property pointing to the owned entity type is no set as required, the owned entity's properties need to be explicitly set to required in order for them to be mapped to the required db columns, but inside EF Core metadata they are still treated the same as per convention? |
There are 2 different things at play here (probably which causes the bug).
Currently we don't have a way to set default value conditionally like that hence currently it sets just null value which makes existing owned type to get "marked" as no longer exist. |
Note, you can manually add a step to the migration to supply default values for existing owned entities: migrationBuilder.Sql(
@"
UPDATE Offers
SET Score_DeliveryTypeScore = 0.0
WHERE Score_Score IS NOT NULL;
"); |
Note from triage: putting this on the backlog to consider doing something better in generated migrations. |
I'm trying to understand what's really happening here and what I can do to work around it. I added a Boolean property to an owned entity, and the migration creates it as a nullable column. So now, when I retrieve existing data, the owned entity is null, apparently because EF can't assign the null value to the Boolean property.
|
So, there are apparently several situations when required columns are created as nullable. This discussion focuses on an "owned entities" case, where "table splitting" causes required columns to be created as nullable. Another scenario involves TPH inheritance (that might just be another "table splitting" scenario - I'm not expert enough to know). From a user perspective, it would be really helpful if we got a yellow highlighted warning in Visual Studio whenever a migration is created that includes mapping of a non-nullable C# type to a nullable column. If there is some reason why code can't (or shouldn't) be scaffolded to insert default (non-null) values in existing data, at least the warning would let us users know that we need to add that code manually. |
@sbsw This issue is specifically about optional dependents sharing a table with the principal where the presence of a non-null value in a column indicates that the dependent exists. It's not about the column being null, but about the impact this has on existing data. This does not apply to other cases, such as TPH columns. |
Description and example
When getting an entity from the database that owns another entity, EF Core returns the whole owned entity as null if one of it's properties has null value in the database.
I have created a small project that reproduces the issue: https://github.com/vzarskus/EFCorePossibleBug. It mimics the real world scenario that me and me colleagues encountered when working with EF Core.
Here is a quick step by step example of how one might encounter this issue:
Provider and version information
EF Core version: 5.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer, however Microsoft.EntityFrameworkCore.Sqlite is used in the example that reproduces the issue, so the problem is probably not related to the database provider.
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio Code 1.58
The text was updated successfully, but these errors were encountered: