Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upDefining which wins if they are different, navigational links or foreign key values? #21105
Milestone
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Summary
I am trying to get a confirmation/denial of the statement "a set navigational properties overrides the foreign key in a detached entity class" because I use this feature to copy entity classes with specific relationships.
Detailed reasoning
While updating the book Entity Framework Core in Action I wrote a unit test (see below) to find out what happens if a navigational property is set to one entity instance, and a foreign key was is set to link to a different entity instance.
(If you need the entity classes I can provide them, but the code is just to explain what I am looking at)
Having this information I copied an entity class with its relationships by
AsNoTrackingHere is the unit test that copies an
Order, with itsLineItems, but doesn't copy theBookentity.NOTE: This only works because the foreign key
OrderIdis overridden by the navigational propertyLineItemsin theOrderentity, but theBookIdforeign key works, because the navigational propertyChosenBookis null.My requested solution
It would be useful if the statement that "a set navigational properties overrides the foreign key in a detached entity class" was confirmed. If that statement isn't true then knowing that is also useful.