Skip to content

Saving Entity with related child entity fails ater update to version 5.04 #24541

Closed as not planned

Description

@KaNarlist

After updating from version 3.1.11 to 5.0.4 I get an error when trying to save a new entity that contains another new entity. The error says

The value of 'Owner.OwnedId' 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.

The relevant part of the configuration for Owner looks like this

builder.Property(e => e.Id)
       .HasColumnName(IdColumnName())
       .ValueGeneratedOnAdd()
       .HasValueGenerator<EntityIdGenerator>();

builder.HasKey(e => e.Id)
       .HasName(KeyName());
	   
builder.Property(p => p.OwnedId)
       .HasColumnName("OWNED_ID");
	   
builder.HasOne(p => p.Owned)
       .WithOne()
       .HasForeignKey<Owner>(p => p.OwnedId)
       .HasPrincipalKey<Owner>(e => e.Id)
       .OnDelete(DeleteBehavior.Cascade);

and Owned like this

builder.Property(e => e.Id)
       .HasColumnName(IdColumnName())
       .ValueGeneratedOnAdd()
       .HasValueGenerator<EntityIdGenerator>();

builder.HasKey(e => e.Id)
       .HasName(KeyName());

I found other threads with the same error saying it only occurs when there is a key that is not generated set to the default value. But both keys have a value generator configured and when I call MyContext.Update(owner) the keys for owner and owned get generated correctly and set to their id fields and the owner.OwnedId field.
Still it says that the Id is unknown for some reason.

Ef core logs out the following:

2021-03-30 08:45:52.9603|17008|ERROR|An error occurred using a transaction.
2021-03-30 08:45:53.0399|17008|DEBUG|'"MyContext"' generated value '10002' for the property '"Id"."Owner"'.
2021-03-30 08:45:53.1502|17008|DEBUG|Context 'MyContext' started tracking 'Owner' entity with key '{Id: 10002}'.
2021-03-30 08:45:53.1502|17008|DEBUG|2021-03-30 08:45:53.154274 ThreadID:4 (ENTRY) OracleValueGeneratorSelector.Select()
2021-03-30 08:45:53.1502|17008|DEBUG|2021-03-30 08:45:53.156156 ThreadID:4 (EXIT) OracleValueGeneratorSelector.Select()
2021-03-30 08:45:54.0278|17008|ERROR|An error occurred using a transaction.
2021-03-30 08:45:54.1114|17008|DEBUG|'"MyContext"' generated value '10001' for the property '"Id"."Owned"'.
2021-03-30 08:45:54.1262|17008|DEBUG|Context 'MyContext' started tracking 'Owned' entity with key '{Id: 10001}'.
2021-03-30 08:46:07.3225|17008|DEBUG|DetectChanges starting for 'MyContext'.
2021-03-30 08:46:07.3368|17008|DEBUG|DetectChanges completed for 'MyContext'.
2021-03-30 08:46:07.3368|17008|DEBUG|SaveChanges starting for 'MyContext'.
2021-03-30 08:46:07.3368|17008|DEBUG|DetectChanges starting for 'MyContext'.
2021-03-30 08:46:07.3456|17008|DEBUG|DetectChanges completed for 'MyContext'.
2021-03-30 08:46:07.3456|17008|DEBUG|2021-03-30 08:46:07.347220 ThreadID:4 (EXIT) OracleExecutionStrategy.ctor()
2021-03-30 08:46:07.3456|17008|DEBUG|2021-03-30 08:46:07.348425 ThreadID:4 (ENTRY) OracleExecutionStrategy.Execute()
2021-03-30 08:46:07.3456|17008|ERROR|2021-03-30 08:46:07.356289 ThreadID:4 (ERROR) OracleExecutionStrategy.Execute() : System.InvalidOperationException: The value of 'Onwer.OwnedId' 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.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PrepareToSave()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave(Boolean cascadeChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
at Oracle.EntityFrameworkCore.Storage.Internal.OracleExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
2021-03-30 08:46:07.3456|17008|DEBUG|2021-03-30 08:46:07.359659 ThreadID:4 (EXIT) OracleExecutionStrategy.Execute()
2021-03-30 08:46:07.3623|17008|ERROR|An exception occurred in the database while saving changes for context type 'MyNamespace.MyContext'.
System.InvalidOperationException: The value of 'Owner.OwnedId' 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.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PrepareToSave()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave(Boolean cascadeChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
at Oracle.EntityFrameworkCore.Storage.Internal.OracleExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)|System.InvalidOperationException: The value of 'Owner.EntityId' 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.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.PrepareToSave()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetEntriesToSave(Boolean cascadeChanges)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
at Oracle.EntityFrameworkCore.Storage.Internal.OracleExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.dll
An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code
The value of 'Owner.OwnedId' 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.

Activity

  1. KaNarlist commented on Mar 30, 2021

    @KaNarlist
    Author

    I just found out that for some reason EntityIdGenerator gets called 3 times. Two times for Owner and one time for Owned.

    But when I add a call to MyContext.Update(owner.Owned) the EntityIdGenerator only gets called 2 times and the error doesn't occur.

    So with this

    MyContext.Update(owner);
    MyContext.SaveChanges();
    

    I get the above error and EntityIdGenerator gets called two times for Owner and one time for Owned.
    But with this

    MyContext.Update(owner.Owned);	   
    MyContext.Update(owner);
    MyContext.SaveChanges();
    

    SaveChanges works without throwing the error and EntityIdGenerator gets called one time for Owner and one time for Owned.

    Something seems strange there.

  2. ajcvickers commented on Mar 31, 2021

    @ajcvickers
    Contributor

    I don't see anything obviously wrong here. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

  3. ajcvickers commented on Apr 9, 2021

    @ajcvickers
    Contributor

    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.

  4. added a commit that references this issue on Jun 19, 2026
    2c99b9a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions