You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core #25359
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:
Create an owned entity (do not make it required as the business case does not require it).
Set values to its properties.
Later decide to expand the owned entity by adding a few more properties to it (do not make them required as the business case does not require it).
Run the migration and update the database. The new properties should have null values in the database columns that represent them.
Run some code that gets the entity from the database and tries to manipulate the owned entity.
Realize that the whole owned entity is returned as null, even though only some of it's properties have null values in the database.
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
In the issue that you are refering to the second rule of retrieving optional owned entity states:
"If first condition fails. we look for non-pk optional properties. if any of them have value we get data from server and materialize instance. At least one non-null value would indicate that instance exists."
However, my example shows the opposite - that if at least one of the properties do not have value, the instance is not materialized.
Find non-pk required properties, if all of them have value then we get data from server and materialize instance. We need value for all required properties else creating instance would throw.
Both the properties - Score and DeliveryTypeScore are required properties in EF Core metadata. Not all of them have value since DeliveryTypeScore has null value. We cannot initialize an instance of owned type because that means assigning null to DeliveryTypeScore which is non-nullable enum type. So from the perspective of query this is working as expected.
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.
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?
When I am generating migrations for the owned entity in the example, the migration sets columns to nullable.
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).
Properties in owned type can be required/optional in C# model but they would be nullable in database (unless owned type is marked as required dependent) because a null value indicates that owned type associated with owner is null. Hence what you are seeing from metadata perspective is correct.
Required properties (in c# model) on owned type must have value in database for those columns when owned type exists. That means the default value for a required column which is being added later gets complicated
If owned type doesn't exist then default value can/should be null.
If owned type does exist then default value must be non-null.
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.
changed the title [-]EF Core gets the whole owned entity as NULL if one of it's properties is NULL[/-][+]Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core.[/+]on Jul 29, 2021
changed the title [-]Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core.[/-][+]Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core[/+]on Jul 29, 2021
I would like to raise attention again to this old issue because I just wasted a couple of hours investigating a bug caused by it in EF Core 10.0.5.
In my opinion, EF Core should do two things:
When scaffolding a migration in this case, generate a stub for the required SQL UPDATE script. If this is hard, at least generate a warning that the developer needs to do something. Currently, EF Core is creating migrations that will generate invalid data.
When loading an owned entity where nullability doesn't match, throw an exception rather than swallowing the issue and pretending the owned entity is null! Swallowing the error will yield to follow-up errors upon updating such as the following (with nested owned entity):
The instance of entity type 'EntityType.OuterOwnedEntity.Property#InnerOwnedEntity' cannot be tracked because another instance with the key value '{OuterOwnedEntityForeignKeyPropertyId: a10a49a0-52a8-49cd-ab95-47fa93585b59}' is already being tracked. When replacing owned entities, modify the properties without changing the instance or detach the previous owned entity entry first.
Obviously, I'm biased because of the time I just spent with this, but both issues feel like severe bugs to me.
When loading an owned entity where nullability doesn't match, throw an exception rather than swallowing the issue and pretending the owned entity is null!
For this, can you please open a new issue with a minimal repro? This issue only tracks the migration side of it, but you seem to be reporting a query loading problem.
It's already stated in the first sentence of this issue: "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."
It's already stated in the first sentence of this issue: "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."
Ah OK, thanks. FWIW if there's a query materialization issue here, that's separate from any migration-related issue - better to distinguish. But let's see a minimal repro and proceed from there.
Activity
smitpatel commentedon Jul 29, 2021
Duplicate of #23564
vzarskus commentedon Jul 29, 2021
In the issue that you are refering to the second rule of retrieving optional owned entity states:
"If first condition fails. we look for non-pk optional properties. if any of them have value we get data from server and materialize instance. At least one non-null value would indicate that instance exists."
However, my example shows the opposite - that if at least one of the properties do not have value, the instance is not materialized.
smitpatel commentedon Jul 29, 2021
Refer the first rule
Both the properties -
ScoreandDeliveryTypeScoreare required properties in EF Core metadata. Not all of them have value sinceDeliveryTypeScorehas null value. We cannot initialize an instance of owned type because that means assigning null toDeliveryTypeScorewhich is non-nullable enum type. So from the perspective of query this is working as expected.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
bricelam commentedon Jul 29, 2021
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.
smitpatel commentedon Jul 29, 2021
Yes, owned type is adding a required property and we generate migration which puts null values for existing columns which should be non-null.
vzarskus commentedon Jul 29, 2021
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?
When I am generating migrations for the owned entity in the example, the migration sets columns to nullable.
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?
smitpatel commentedon Jul 29, 2021
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.
[-]EF Core gets the whole owned entity as NULL if one of it's properties is NULL[/-][+]Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core.[/+][-]Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core.[/-][+]Migrations: Adding a required property on owned type makes existing data to appear deleted from EF Core[/+]bricelam commentedon Jul 29, 2021
Note, you can manually add a step to the migration to supply default values for existing owned entities:
18 remaining items
fschmied commentedon Apr 21, 2026
I would like to raise attention again to this old issue because I just wasted a couple of hours investigating a bug caused by it in EF Core 10.0.5.
In my opinion, EF Core should do two things:
Obviously, I'm biased because of the time I just spent with this, but both issues feel like severe bugs to me.
roji commentedon Apr 21, 2026
For this, can you please open a new issue with a minimal repro? This issue only tracks the migration side of it, but you seem to be reporting a query loading problem.
fschmied commentedon Apr 21, 2026
It's already stated in the first sentence of this issue: "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."
But I'll try to get a minimal repro done. 👍
roji commentedon Apr 21, 2026
Ah OK, thanks. FWIW if there's a query materialization issue here, that's separate from any migration-related issue - better to distinguish. But let's see a minimal repro and proceed from there.
fschmied commentedon May 5, 2026
@roji Here it goes: #38223.