// <summary> // Creates an EntityKey for a principal entity based on the foreign key values contained // in this entity. This implies that this entity is at the dependent end of the relationship. // </summary> // <param name="dependentEntry"> The EntityEntry for the dependent that contains the FK </param> // <param name="constraint"> The constraint that describes this FK relationship </param> // <param name="principalEntitySet"> The entity set at the principal end of the relationship </param> // <param name="useOriginalValues"> If true then the key will be constructed from the original FK values </param> // <returns> The key, or null if any value in the key is null </returns> public static EntityKey CreateKeyFromForeignKeyValues( EntityEntry dependentEntry, ReferentialConstraint constraint, EntitySet principalEntitySet, bool useOriginalValues) { // Build the key values. If any part of the key is null, then the entire key // is considered null. var dependentProps = constraint.ToProperties; var numValues = dependentProps.Count; if (numValues == 1) { var keyValue = useOriginalValues ? dependentEntry.GetOriginalEntityValue(dependentProps.First().Name) : dependentEntry.GetCurrentEntityValue(dependentProps.First().Name); return(keyValue == DBNull.Value ? null : new EntityKey(principalEntitySet, keyValue)); } // Note that the properties in the principal entity set may be in a different order than // they appear in the constraint. Therefore, we create name value mappings to ensure that // the correct values are associated with the correct properties. // Unfortunately, there is not way to call the public EntityKey constructor that takes pairs // because the internal "object" constructor hides it. Even this doesn't work: // new EntityKey(principalEntitySet, (IEnumerable<KeyValuePair<string, object>>)keyValues) var keyNames = principalEntitySet.ElementType.KeyMemberNames; Debug.Assert(keyNames.Length == numValues, "Number of entity set key names does not match constraint names"); var values = new object[numValues]; var principalProps = constraint.FromProperties; for (var i = 0; i < numValues; i++) { var value = useOriginalValues ? dependentEntry.GetOriginalEntityValue(dependentProps[i].Name) : dependentEntry.GetCurrentEntityValue(dependentProps[i].Name); if (value == DBNull.Value) { return(null); } var keyIndex = Array.IndexOf(keyNames, principalProps[i].Name); Debug.Assert(keyIndex >= 0 && keyIndex < numValues, "Could not find constraint prop name in entity set key names"); values[keyIndex] = value; } return(new EntityKey(principalEntitySet, values)); }
public static EntityKey CreateKeyFromForeignKeyValues( EntityEntry dependentEntry, ReferentialConstraint constraint, EntitySet principalEntitySet, bool useOriginalValues) { ReadOnlyMetadataCollection <EdmProperty> toProperties = constraint.ToProperties; int count = toProperties.Count; if (count == 1) { object singletonKeyValue = useOriginalValues ? dependentEntry.GetOriginalEntityValue(toProperties.First <EdmProperty>().Name) : dependentEntry.GetCurrentEntityValue(toProperties.First <EdmProperty>().Name); if (singletonKeyValue != DBNull.Value) { return(new EntityKey((EntitySetBase)principalEntitySet, singletonKeyValue)); } return((EntityKey)null); } string[] keyMemberNames = principalEntitySet.ElementType.KeyMemberNames; object[] compositeKeyValues = new object[count]; ReadOnlyMetadataCollection <EdmProperty> fromProperties = constraint.FromProperties; for (int index1 = 0; index1 < count; ++index1) { object obj = useOriginalValues ? dependentEntry.GetOriginalEntityValue(toProperties[index1].Name) : dependentEntry.GetCurrentEntityValue(toProperties[index1].Name); if (obj == DBNull.Value) { return((EntityKey)null); } int index2 = Array.IndexOf <string>(keyMemberNames, fromProperties[index1].Name); compositeKeyValues[index2] = obj; } return(new EntityKey((EntitySetBase)principalEntitySet, compositeKeyValues)); }
/// <summary> /// Creates an EntityKey for a principal entity based on the foreign key values contained /// in this entity. This implies that this entity is at the dependent end of the relationship. /// </summary> /// <param name="dependentEntry"> The EntityEntry for the dependent that contains the FK </param> /// <param name="constraint"> The constraint that describes this FK relationship </param> /// <param name="principalEntitySet"> The entity set at the principal end of the the relationship </param> /// <param name="useOriginalValues"> If true then the key will be constructed from the original FK values </param> /// <returns> The key, or null if any value in the key is null </returns> public static EntityKey CreateKeyFromForeignKeyValues( EntityEntry dependentEntry, ReferentialConstraint constraint, EntitySet principalEntitySet, bool useOriginalValues) { // Build the key values. If any part of the key is null, then the entire key // is considered null. var dependentProps = constraint.ToProperties; var numValues = dependentProps.Count; if (numValues == 1) { var keyValue = useOriginalValues ? dependentEntry.GetOriginalEntityValue(dependentProps.First().Name) : dependentEntry.GetCurrentEntityValue(dependentProps.First().Name); return keyValue == DBNull.Value ? null : new EntityKey(principalEntitySet, keyValue); } // Note that the properties in the principal entity set may be in a different order than // they appear in the constraint. Therefore, we create name value mappings to ensure that // the correct values are associated with the correct properties. // Unfortunately, there is not way to call the public EntityKey constructor that takes pairs // because the internal "object" constructor hides it. Even this doesn't work: // new EntityKey(principalEntitySet, (IEnumerable<KeyValuePair<string, object>>)keyValues) var keyNames = principalEntitySet.ElementType.KeyMemberNames; Debug.Assert(keyNames.Length == numValues, "Number of entity set key names does not match constraint names"); var values = new object[numValues]; var principalProps = constraint.FromProperties; for (var i = 0; i < numValues; i++) { var value = useOriginalValues ? dependentEntry.GetOriginalEntityValue(dependentProps[i].Name) : dependentEntry.GetCurrentEntityValue(dependentProps[i].Name); if (value == DBNull.Value) { return null; } var keyIndex = Array.IndexOf(keyNames, principalProps[i].Name); Debug.Assert(keyIndex >= 0 && keyIndex < numValues, "Could not find constraint prop name in entity set key names"); values[keyIndex] = value; } return new EntityKey(principalEntitySet, values); }