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

DetectChanges works incorrect when update nested owned entity #29085

Closed
KyMback opened this issue Sep 14, 2022 · 3 comments
Closed

DetectChanges works incorrect when update nested owned entity #29085

KyMback opened this issue Sep 14, 2022 · 3 comments
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@KyMback
Copy link

KyMback commented Sep 14, 2022

When I try to assign null to nested owned entity after dbContext.ChangeTracker.DetectChanges() ef core assigns to this property previous value (I guess snapshot value)

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddDbContext<MainDbContext>(e => e.UseInMemoryDatabase("DB"));
var provider = services.BuildServiceProvider();
var dbContext = provider.GetRequiredService<MainDbContext>();

dbContext.Add(new Entity
{
    NestedEntity = new NestedEntity
    {
        NestedNestedEntity1 = new NestedNestedEntity1
        {
            Value = "some value1",
        },
        NestedNestedEntity2 = new NestedNestedEntity2
        {
            Value = "some value2"
        }
    },
});

dbContext.SaveChanges();

var entity = dbContext.Set<Entity>().First();

Write(entity);

entity.NestedEntity = new NestedEntity
{
    NestedNestedEntity1 = new NestedNestedEntity1
    {
        Value = "some new value1",
    },
    NestedNestedEntity2 = null,
};

Write(entity);

dbContext.ChangeTracker.DetectChanges();

Write(entity);

void Write(Entity e)
{
    Console.WriteLine("NestedNestedEntity1: " + e.NestedEntity.NestedNestedEntity1?.Value);
    Console.WriteLine("NestedNestedEntity2: " + e.NestedEntity.NestedNestedEntity2?.Value);
    Console.WriteLine();
}

public class MainDbContext : DbContext
{
    public MainDbContext(DbContextOptions<MainDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Entity>(builder =>
        {
            builder.OwnsOne(e => e.NestedEntity, n1 =>
            {
                n1.OwnsOne(e => e.NestedNestedEntity1, n2 =>
                {
                    n2.Property(e => e.Value).IsRequired();
                });
                n1.OwnsOne(e => e.NestedNestedEntity2, n2 =>
                {
                    n2.Property(e => e.Value).IsRequired();
                });
            });
        });
        
        base.OnModelCreating(modelBuilder);
    }
}

public class Entity
{
    public Guid Id { get; set; }
    public NestedEntity NestedEntity { get; set; }
}

public class NestedEntity
{
    public NestedNestedEntity1? NestedNestedEntity1 { get; set; }
    public NestedNestedEntity2? NestedNestedEntity2 { get; set; }
}

public class NestedNestedEntity1
{
    public string Value { get; set; }
}

public class NestedNestedEntity2
{
    public string Value { get; set; }
}

Output

NestedNestedEntity1: some value1
NestedNestedEntity2: some value2

NestedNestedEntity1: some new value1
NestedNestedEntity2:

NestedNestedEntity1: some new value1
NestedNestedEntity2: some value2

I expect that NestedNestedEntity2 should be also null after DetectChanges but it is what it is

Provider and version information

EF Core version: 6.0.9
Database provider: Microsoft.EntityFrameworkCore.InMemory, Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0

@AndriySvyryd
Copy link
Member

This is likely fixed in 7.0.0-rc2

@KyMback
Copy link
Author

KyMback commented Sep 16, 2022

Will it be fixed in .net 6? If yes when?

@ajcvickers
Copy link
Member

Note for triage: still repros on latest daily.

@ajcvickers ajcvickers self-assigned this Sep 17, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Sep 17, 2022
ajcvickers added a commit that referenced this issue Sep 18, 2022
The new nested entity was being fixed up to the deleted double-nested entities.

Fixes #29085
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 18, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc2 Sep 19, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

No branches or pull requests

3 participants