aspnet/EntityFramework — pull request #1304 by ajcvickers (2014-12-16 23:56:58+00:00)
Title: Allow principals and dependents using key generation to be added in any Order

See issues #1271 and #1207. The main issue here is that if a dependent entity is added to the context before its principal has been added then the primary key value of the principal has not yet been generated. This means that people need to be careful which order to add things in, which in turn means that AttachGraph would not work in some scenarios,

The fix is to find the value generator for the principal of the relationship (possibly traversing several relationships to do so) and use it to generate a temporary key for the dependent entity. When the principal is finally added fixup will happen and the actual principal value will be propagated down to the dependents. The value generator for the ultimate principal needs to be used (instead of some other generator) so that key conflicts in the generated values do not happen as values propagate down.