private static void AssignAnyMismatchedExplicitTransitiveForeignKeyValues(object obj, IList <Tuple <object, DtoMetadata> > ancestors, ForeignKeyReferenceAttribute fkAttribute, Type propertyType, PropertyMetadata property) { var targetObjectType = fkAttribute.ReferencedDto; // We don't look at the immediate ancestor because such links should already // have been wired up; we're only looking for possible target types further // back up the chain of ancestors. if (ancestors.Count > 2) { for (var index = ancestors.Count - 3; index >= 0; --index) { var ancestor = ancestors[index]; var ancestorMetadata = ancestor.Item2; if (ancestorMetadata.DtoType != targetObjectType) { continue; } var ancestorPkColumn = ancestorMetadata.PrimaryKey; if (ancestorPkColumn == null) { // There's nothing we can assign to the foreign key value for any // ancestor of the expected type. break; } if (!propertyType.IsAssignableFrom(ancestorPkColumn.Prop.PropertyType)) { continue; } var pkValue = ancestorMetadata.GetPrimaryKeyValueAsObject(ancestor); var fkValue = property.Prop.GetValue(obj); if (pkValue != fkValue && (pkValue == null || fkValue == null || !pkValue.Equals(fkValue))) { property.SetValue(obj, pkValue); } } } }
private static void AssignAnyMismatchedExplicitTransitiveForeignKeyValues(object obj, IList<Tuple<object, DtoMetadata>> ancestors, ForeignKeyReferenceAttribute fkAttribute, Type propertyType, PropertyMetadata property) { var targetObjectType = fkAttribute.ReferencedDto; // We don't look at the immediate ancestor because such links should already // have been wired up; we're only looking for possible target types further // back up the chain of ancestors. if (ancestors.Count > 2) { for (var index = ancestors.Count - 3; index >= 0; --index) { var ancestor = ancestors[index]; var ancestorMetadata = ancestor.Item2; if (ancestorMetadata.DtoType != targetObjectType) { continue; } var ancestorPkColumn = ancestorMetadata.PrimaryKey; if (ancestorPkColumn == null) { // There's nothing we can assign to the foreign key value for any // ancestor of the expected type. break; } if (!propertyType.IsAssignableFrom(ancestorPkColumn.Prop.PropertyType)) { continue; } var pkValue = ancestorMetadata.GetPrimaryKeyValueAsObject(ancestor); var fkValue = property.Prop.GetValue(obj); if (pkValue != fkValue && (pkValue == null || fkValue == null || !pkValue.Equals(fkValue))) { property.SetValue(obj, pkValue); } } } }