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

[Bug]: The Property uses ValueGenerator in complex FK is not tracked correctly #26332

Closed
mseada94 opened this issue Oct 13, 2021 · 4 comments · Fixed by #25652
Closed

[Bug]: The Property uses ValueGenerator in complex FK is not tracked correctly #26332

mseada94 opened this issue Oct 13, 2021 · 4 comments · Fixed by #25652
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@mseada94
Copy link

Setup

  • Two entities having one-to-many relation with a composite foreign key. (One Author Has Many Books)
  • The relation is optional and not required.
  • One of the key properties is generated using custom ValueGenerator<Guid>. (TenantId)

Error and notes

When adding a book with no author without setting the TenantId

  • The value has been generated and could be shown as the CurrentValue for TenantId prop when debugging the EntityEntry
  • The following error is returned:
The value of 'Book.TenantId' is unknown when attempting to save changes. This is because the property is also part of a foreign key for which the principal entity in the relationship is not known.

However, When adding a book with no author and **setting the TenantId Manually, Succeded with no errors.

I noticed a difference when debugging EntityEntry.InternalEntry.IsKeyUnknown is set with true in the first case and false in the second case.

Code Sample (Console App)
https://github.com/mohamed-seada-1994/EFCore-ValueGenerator-Bug-Sample

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 5.0
Operating system: Win10
IDE: Visual Studio 2019 16.9.6

@ajcvickers
Copy link
Member

@mohamed-seada-1994 By default, EF Core assumes value generators generate values that can change from run to run. Starting in EF Core 6.0, this can be overridden on the value generator with GeneratesStableValues. For example:

    public class TenantIdGenerator : ValueGenerator<Guid>
    {
        public override Guid Next(EntityEntry entry)
            => Guid.Parse("98D06A82-C691-4988-EA39-08D98E2C8D8F");

        public override bool GeneratesTemporaryValues => false;

        public override bool GeneratesStableValues => true;
    }

I tested with your code using the EF Core 6.0 RC2 release and it results in both test cases passing.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Oct 13, 2021
@mseada94
Copy link
Author

@ajcvickers Thank you for your answer.

For now, I need to find a solution for EF Core 5,
I am thinking of iterating over the added EntityEntry and resetting the property value on saveChanges

addedEntityEntry.Property("TenantId").CurrentValue = addedEntityEntry.Property("TenantId").CurrentValue;

I test this solution and it works.
Could this workaround cause any issues? and Is there a better workaround?

@ajcvickers
Copy link
Member

@mohamed-seada-1994 That seems reasonable.

@mseada94
Copy link
Author

@ajcvickers Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants