Skip to content

Shadow key property not known error after property set by value generator #27472

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

Closed
AndrewTriesToCode opened this issue Feb 18, 2022 · 2 comments

Comments

@AndrewTriesToCode
Copy link

AndrewTriesToCode commented Feb 18, 2022

I have a sample project to reproduce the issue. The unit test will see an exception System.InvalidOperationException The value of shadow key property 'MyEntity.ShadowKey' is unknown when attempting to save changes. Although the value should be known because I have a value generator in place and the debugger confirms it is used prior to the error.

Also tested for .NET Core 3.1 and .NET 5.0 -- similar error but slightly different wording.

This issue seems similar to #26330 but this isn't an owned entity situation.

Thanks!

The entity:

public class MyEntity
{
    public int Key1;
}

The DbContext:

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<MyEntity> MyEntity { get; set; }
    public string ShadowKeyValueOnGenerate { get; set; } = "";

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyEntity>().Property<string?>("ShadowKey").HasValueGenerator<ShadowKeyGenerator>();
        modelBuilder.Entity<MyEntity>().HasKey("Key1", "ShadowKey");
    }
}

Te value generator:

public class ShadowKeyGenerator : ValueGenerator<string?>
{
    public override string? Next(EntityEntry entry)
    {
        // This runs right before the error saying the value isn't known...
        return ((MyDbContext)entry.Context).ShadowKeyValueOnGenerate;
    }

    public override bool GeneratesTemporaryValues => false;
}

The test:

[Fact]
public void RemoveFreshlyAttachedEntry()
{
    var context = new MyDbContext(_options);
    context.ShadowKeyValueOnGenerate = "value1";
    
    var entity1 = new MyEntity { Key1 = 1 };
    
    // Error here that the shadow key property isn't known although the generator has provided the value...
    context.Remove(entity1);
    
    context.SaveChanges();
    
    Assert.Empty(context.MyEntity.ToList());
}
@ajcvickers
Copy link
Contributor

@AndrewTriesToCode Currently value generators are only run when entities are put in the Added state--usually when Add is called. They are not run when Attach, Update, or Remove are called. Please vote (👍) for #6999 to indicate that running generators at other times is important to you.

@AndrewTriesToCode
Copy link
Author

Thanks for taking a look. I gave my thumbs up on that issue.

@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

2 participants