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

"The value of shadow key property 'PROPERTYNAME' is unknown when attempting to save changes" on tracked entities #26330

Closed
fdipuma opened this issue Oct 13, 2021 · 4 comments
Labels
area-change-tracking area-save-changes 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

@fdipuma
Copy link

fdipuma commented Oct 13, 2021

Hello!

We are detecting a regression on our tests when using collections of Owned Types with a shadow key property.

It seems the owned entity is detected as with "unknown key" even if it is still tracked on the context (this is not happening with EF Core 3.1.x)

Tested with InMemory provider.

Full Program.cs:

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

var dbContextOptions = new DbContextOptionsBuilder<TestDbContext>().UseInMemoryDatabase("test").Options;
using var context = new TestDbContext(dbContextOptions);

var user = new User();
var firstAddress = new Address { City = "Milan"};
user.Addresses.Add(firstAddress);
context.Add(user);
context.SaveChanges();

user.Addresses.Remove(firstAddress);
user.Addresses.Add(new Address { City = "Rome"});
context.SaveChanges(); // System.InvalidOperationException: The value of shadow key property 'Address.Id' is unknown when attempting to save changes.

public class User
{
    public int Id { get; set; }
    public ICollection<Address> Addresses { get; set; } = new List<Address>();
}

public class Address
{
    public string City { get; set; }
}

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        var user = modelBuilder.Entity<User>();
        user.HasKey(u => u.Id);
        user.OwnsMany(e => e.Addresses, owned =>
        {
            owned.HasKey("Id"); // shadow prop
            owned.Property("Id");
        });
    }
}

Stack trace

Unhandled exception. System.InvalidOperationException: The value of shadow key property 'Address.Id' is unknown when attempting to save changes. This is because shadow property values cannot be preserved when the entity is not being tracked. Consider adding the property
to the entity's .NET type. See https://aka.ms/efcore-docs-owned-collections for more information.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.<PrepareToSave>g__CheckForUnknownKey|98_0(IProperty property, <>c__DisplayClass98_0& )
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PrepareToSave()
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave(Boolean cascadeChanges)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__104_0(DbContext _, ValueTuple`2 t)
   at Microsoft.EntityFrameworkCore.Storage.NonRetryingExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at Program.<Main>$(String[] args) in D:\Dev\dotNET\ef-core-shadow-prop\EFShadowPropIssue\EFShadowPropIssue\Program.cs:line 15

Provider and version information

EF Core version: 6.0.0-rc.2.21480.5
Database providers: Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET 6.0
Operating system: Windows 10.0.19042.1288
IDE: JetBrains Rider 2021.2.1

@ajcvickers
Copy link
Member

@AndriySvyryd Verified this is a regression from 5.0, and still repros on latest daily.

@ajcvickers
Copy link
Member

@AndriySvyryd FYI, started working on this; will continue tomorrow.

@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 Oct 14, 2021
@ajcvickers ajcvickers added this to the 6.0.0 milestone Oct 14, 2021
@AndrewTriesToCode
Copy link

AndrewTriesToCode commented Feb 16, 2022

@ajcvickers I'm seeing this currently in EFCore 6.0.2 with Sqlite. Can you confirm it should still be resolved?

var modelToDelete = new ShadowTenantIdModel { Title = "TestTitle", Username = "TestUsername");
newTenant1Context.ShadowTenantIdModels.Remove(modelToDelete);
newTenant1Context.SaveChanges(); // Error about TenantId not being known, although it is set via a nontemp generator

Thanks

@ajcvickers
Copy link
Member

@AndrewTriesToCode Please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking area-save-changes 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