示例#1
0
        private static InternalEntityEntry?TryPropagateValue(InternalEntityEntry entry, IProperty property, IProperty?generationProperty)
        {
            var entityType   = entry.EntityType;
            var stateManager = entry.StateManager;

            foreach (var foreignKey in entityType.GetForeignKeys())
            {
                for (var propertyIndex = 0; propertyIndex < foreignKey.Properties.Count; propertyIndex++)
                {
                    if (property == foreignKey.Properties[propertyIndex])
                    {
                        var principal = foreignKey.DependentToPrincipal == null
                            ? null
                            : entry[foreignKey.DependentToPrincipal];
                        InternalEntityEntry?principalEntry = null;
                        if (principal != null)
                        {
                            principalEntry = stateManager.GetOrCreateEntry(principal, foreignKey.PrincipalEntityType);
                        }
                        else if (foreignKey.PrincipalToDependent != null)
                        {
                            foreach (var danglerEntry in stateManager.GetRecordedReferrers(entry.Entity, clear: false))
                            {
                                if (danglerEntry.Item1 == foreignKey.PrincipalToDependent)
                                {
                                    principalEntry = danglerEntry.Item2;
                                    break;
                                }
                            }
                        }

                        if (principalEntry != null)
                        {
                            var principalProperty = foreignKey.PrincipalKey.Properties[propertyIndex];

                            if (principalProperty != property)
                            {
                                var principalValue = principalEntry[principalProperty];
                                if (generationProperty == null ||
                                    !principalProperty.ClrType.IsDefaultValue(principalValue))
                                {
                                    entry.PropagateValue(principalEntry, principalProperty, property);

                                    return(principalEntry);
                                }
                            }
                        }

                        break;
                    }
                }
            }

            return(null);
        }