Skip to content
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

Owned type change not persisting if original data is null #20421

Closed
RafaelSalguero opened this issue Mar 26, 2020 · 1 comment
Closed

Owned type change not persisting if original data is null #20421

RafaelSalguero opened this issue Mar 26, 2020 · 1 comment

Comments

@RafaelSalguero
Copy link

RafaelSalguero commented Mar 26, 2020

When updating an existing owned type where originally all it's properties are null, the update is not detected by the ChangeTracker, but if a single column is assigned to a not-null value in the database, the update is executed as normally.

Steps to reproduce

  • Create an owned type where all of its properties can be null
  • INSERT a row with only null values and another with some non-null values
  • Updating the owned type property for the all-nulls rows doesn't do anything
  • The same code for the 2nd row with some non-null values does update
class MyTable {
 public int IdMyTable { get; set; }
 public MyOwnedType Owned { get; set; } = new MyOwnedType()
}

[Owned]
class MyOwnedType {
  public string Prop1 { get; set; }
  public string Prop2 { get; set; }
}

var row = c.MyTable.Where(x => x.IdMyTable == 1).First();

//This is done on the database only if the original row has any non-null value in owned type related columns
row.Owned.Prop1 = "hello";

//If we inspect the ChangeTracker, 2 entities are tracked for the row with non-nulls but only 1 with the all nulls row
c.SaveChanges();

Further technical details

EF Core version: 3.1.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.5.0

@AndriySvyryd
Copy link
Member

Duplicate of #18007
Don't initialize MyTable.Owned in the constructor instead do it in the getter: #20213 (comment)

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants