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

DbContext.Update doesn't track added owned entities #19884

Closed
the-ress opened this issue Feb 11, 2020 · 3 comments
Closed

DbContext.Update doesn't track added owned entities #19884

the-ress opened this issue Feb 11, 2020 · 3 comments

Comments

@the-ress
Copy link

the-ress commented Feb 11, 2020

The documentation for DbContext.Update says

A recursive search of the navigation properties will be performed to find reachable entities that are not already being tracked by the context. All entities found will be tracked by the context.

but that's not what I'm observing with owned entities.

Steps to reproduce

Run this code:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        var context = new TestDbContext();

        var parent = new Parent();
        context.Add(parent);
        context.SaveChanges();

        parent.Children.Add(new Child());
        context.Update(parent);
        //context.ChangeTracker.DetectChanges();

        var state = context.Entry(parent.Children[0]).State;
        Console.WriteLine(state); // Writes: Detached
    }
}

class TestDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseInMemoryDatabase(nameof(TestDbContext));

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        => modelBuilder.Entity<Parent>().OwnsMany(x => x.Children);
}

class Parent
{
    public Guid Id { get; set; }
    public IList<Child> Children { get; } = new List<Child>();
}

class Child
{
    public Guid Id { get; set; }
}
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.1" />

I expect the status to be Added, but it's Detached. (If I call context.ChangeTracker.DetectChanges(), the state gets changed to Added.)

Is my expectation correct, or am I misunderstanding the documentation?

Further technical details

EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET Core 3.1
Operating system: Windows 10 (1803)
IDE: Visual Studio 2019 16.4.4

@AndriySvyryd
Copy link
Member

@the-ress The described behavior only happens when the parent is not being tracked when you call Update
The feature that might help your scenario is #10551

@ajcvickers
Copy link
Member

@the-ress It seems likely that a call to Update is not appropriate here--see https://blog.oneunicorn.com/2020/01/17/dontcallupdate/ for more details.

If this isn't the case, then can you explain a bit more about what you are doing that requires the call to Update?

@ajcvickers
Copy link
Member

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

@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