Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Objects / Internal / ForeignKeyFactory.cs / 1305376 / ForeignKeyFactory.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.Metadata.Edm; using System.Diagnostics; using System.Data.Objects.DataClasses; namespace System.Data.Objects.Internal { internal class ForeignKeyFactory { private const string s_NullPart = "EntityHasNullForeignKey"; private const string s_NullForeignKey = "EntityHasNullForeignKey.EntityHasNullForeignKey"; ////// Returns true if the supplied key represents a Conceptual Null /// /// The key to be checked public static bool IsConceptualNullKey(EntityKey key) { if (key == null) { return false; } return string.Equals(key.EntityContainerName, s_NullPart) && string.Equals(key.EntitySetName, s_NullPart); } ////// Checks if the Real Key represents different FK values /// than those present when the Conceptual Null was created /// /// The key representing the Conceptual Null /// The key to be checked ///True if the values are different, false otherwise public static bool IsConceptualNullKeyChanged(EntityKey conceptualNullKey, EntityKey realKey) { Debug.Assert(IsConceptualNullKey(conceptualNullKey), "The key supplied is not a null key"); if (realKey == null) { return true; } return !EntityKey.InternalEquals(conceptualNullKey, realKey, compareEntitySets: false); } ////// Creates an EntityKey that represents a Conceptual Null /// /// An EntityKey representing the existing FK values that could not be nulled ///EntityKey marked as a conceptual null with the FK values from the original key public static EntityKey CreateConceptualNullKey(EntityKey originalKey) { Debug.Assert(originalKey != null, "Original key can not be null"); //Conceptual nulls have special entity set name and a copy of the previous values EntityKey nullKey = new EntityKey(s_NullForeignKey, originalKey.EntityKeyValues); return nullKey; } ////// 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. /// /// The EntityEntry for the dependent that contains the FK /// Identifies the principal end for which a key is required ///The key, or null if any value in the key is null public static EntityKey CreateKeyFromForeignKeyValues(EntityEntry dependentEntry, RelatedEnd relatedEnd) { // Note: there is only ever one constraint per association type ReferentialConstraint constraint = ((AssociationType)relatedEnd.RelationMetadata).ReferentialConstraints.First(); Debug.Assert(constraint.FromRole.Identity == relatedEnd.TargetRoleName, "Unexpected constraint role"); return CreateKeyFromForeignKeyValues(dependentEntry, constraint, relatedEnd.GetTargetEntitySetFromRelationshipSet(), useOriginalValues: false); } ////// 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. /// /// The EntityEntry for the dependent that contains the FK /// The constraint that describes this FK relationship /// The entity set at the principal end of the the relationship /// If true then the key will be constructed from the original FK values ///The key, or null if any value in the key is null public static EntityKey CreateKeyFromForeignKeyValues(EntityEntry dependentEntry, ReferentialConstraint constraint, EntitySet principalEntitySet, bool useOriginalValues) { // Build the key values. If any component of the key is null, then the entire key // is considered null. var dependentProps = constraint.ToProperties; int numValues = dependentProps.Count; if (numValues == 1) { object 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>)keyValues) string[] keyNames = principalEntitySet.ElementType.KeyMemberNames; Debug.Assert(keyNames.Length == numValues, "Number of entity set key names does not match constraint names"); object[] values = new object[numValues]; var principalProps = constraint.FromProperties; for (int i = 0; i < numValues; i++) { object value = useOriginalValues ? dependentEntry.GetOriginalEntityValue(dependentProps[i].Name) : dependentEntry.GetCurrentEntityValue(dependentProps[i].Name); if (value == DBNull.Value) { return null; } int 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); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.Metadata.Edm; using System.Diagnostics; using System.Data.Objects.DataClasses; namespace System.Data.Objects.Internal { internal class ForeignKeyFactory { private const string s_NullPart = "EntityHasNullForeignKey"; private const string s_NullForeignKey = "EntityHasNullForeignKey.EntityHasNullForeignKey"; /// /// Returns true if the supplied key represents a Conceptual Null /// /// The key to be checked public static bool IsConceptualNullKey(EntityKey key) { if (key == null) { return false; } return string.Equals(key.EntityContainerName, s_NullPart) && string.Equals(key.EntitySetName, s_NullPart); } ////// Checks if the Real Key represents different FK values /// than those present when the Conceptual Null was created /// /// The key representing the Conceptual Null /// The key to be checked ///True if the values are different, false otherwise public static bool IsConceptualNullKeyChanged(EntityKey conceptualNullKey, EntityKey realKey) { Debug.Assert(IsConceptualNullKey(conceptualNullKey), "The key supplied is not a null key"); if (realKey == null) { return true; } return !EntityKey.InternalEquals(conceptualNullKey, realKey, compareEntitySets: false); } ////// Creates an EntityKey that represents a Conceptual Null /// /// An EntityKey representing the existing FK values that could not be nulled ///EntityKey marked as a conceptual null with the FK values from the original key public static EntityKey CreateConceptualNullKey(EntityKey originalKey) { Debug.Assert(originalKey != null, "Original key can not be null"); //Conceptual nulls have special entity set name and a copy of the previous values EntityKey nullKey = new EntityKey(s_NullForeignKey, originalKey.EntityKeyValues); return nullKey; } ////// 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. /// /// The EntityEntry for the dependent that contains the FK /// Identifies the principal end for which a key is required ///The key, or null if any value in the key is null public static EntityKey CreateKeyFromForeignKeyValues(EntityEntry dependentEntry, RelatedEnd relatedEnd) { // Note: there is only ever one constraint per association type ReferentialConstraint constraint = ((AssociationType)relatedEnd.RelationMetadata).ReferentialConstraints.First(); Debug.Assert(constraint.FromRole.Identity == relatedEnd.TargetRoleName, "Unexpected constraint role"); return CreateKeyFromForeignKeyValues(dependentEntry, constraint, relatedEnd.GetTargetEntitySetFromRelationshipSet(), useOriginalValues: false); } ////// 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. /// /// The EntityEntry for the dependent that contains the FK /// The constraint that describes this FK relationship /// The entity set at the principal end of the the relationship /// If true then the key will be constructed from the original FK values ///The key, or null if any value in the key is null public static EntityKey CreateKeyFromForeignKeyValues(EntityEntry dependentEntry, ReferentialConstraint constraint, EntitySet principalEntitySet, bool useOriginalValues) { // Build the key values. If any component of the key is null, then the entire key // is considered null. var dependentProps = constraint.ToProperties; int numValues = dependentProps.Count; if (numValues == 1) { object 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>)keyValues) string[] keyNames = principalEntitySet.ElementType.KeyMemberNames; Debug.Assert(keyNames.Length == numValues, "Number of entity set key names does not match constraint names"); object[] values = new object[numValues]; var principalProps = constraint.FromProperties; for (int i = 0; i < numValues; i++) { object value = useOriginalValues ? dependentEntry.GetOriginalEntityValue(dependentProps[i].Name) : dependentEntry.GetCurrentEntityValue(dependentProps[i].Name); if (value == DBNull.Value) { return null; } int 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); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BookmarkScope.cs
- tibetanshape.cs
- SqlCharStream.cs
- AppSettingsExpressionBuilder.cs
- DirectionalLight.cs
- XmlNode.cs
- TransportContext.cs
- ToolStripContainerActionList.cs
- EventWaitHandleSecurity.cs
- GroupByExpressionRewriter.cs
- ResourcePermissionBaseEntry.cs
- AsynchronousChannel.cs
- XmlSchemaInferenceException.cs
- LinqDataSourceStatusEventArgs.cs
- SoapProcessingBehavior.cs
- PeerCredentialElement.cs
- _NTAuthentication.cs
- ToolTipService.cs
- RedirectionProxy.cs
- CompilationUtil.cs
- ReversePositionQuery.cs
- DetailsViewPagerRow.cs
- TempFiles.cs
- InstanceOwner.cs
- DefaultMemberAttribute.cs
- EntryWrittenEventArgs.cs
- ExternalException.cs
- SQLGuid.cs
- Page.cs
- TemplateXamlTreeBuilder.cs
- ConfigXmlSignificantWhitespace.cs
- ActionMessageFilterTable.cs
- MouseCaptureWithinProperty.cs
- ProviderCollection.cs
- Graphics.cs
- LocalValueEnumerator.cs
- TreeNode.cs
- WebServiceData.cs
- BindingListCollectionView.cs
- SchemaNamespaceManager.cs
- TransactionInformation.cs
- HtmlAnchor.cs
- EndpointAddressAugust2004.cs
- DataObject.cs
- GetPageNumberCompletedEventArgs.cs
- CodeIdentifier.cs
- CodeGenerator.cs
- SerializationException.cs
- SoapFault.cs
- HMACSHA1.cs
- BuiltInPermissionSets.cs
- XmlBaseReader.cs
- DesignerTextBoxAdapter.cs
- SerializationInfo.cs
- XmlReflectionImporter.cs
- DataGridPreparingCellForEditEventArgs.cs
- CultureTableRecord.cs
- TextTreePropertyUndoUnit.cs
- WindowsPen.cs
- documentsequencetextcontainer.cs
- FrameworkContentElement.cs
- DeleteWorkflowOwnerCommand.cs
- CaseInsensitiveComparer.cs
- SortableBindingList.cs
- CatalogPartChrome.cs
- TrackingRecord.cs
- RegexFCD.cs
- Cursors.cs
- XPathDescendantIterator.cs
- Soap.cs
- UniqueConstraint.cs
- DomNameTable.cs
- CatalogZone.cs
- PropertyMap.cs
- ImmutablePropertyDescriptorGridEntry.cs
- serverconfig.cs
- AuthorizationRuleCollection.cs
- DataGridViewLayoutData.cs
- ArglessEventHandlerProxy.cs
- MetadataPropertyAttribute.cs
- DesignerTransactionCloseEvent.cs
- SpotLight.cs
- SecurityDocument.cs
- PreviewKeyDownEventArgs.cs
- WindowsAuthenticationModule.cs
- SimpleRecyclingCache.cs
- XmlILConstructAnalyzer.cs
- EntityClassGenerator.cs
- Predicate.cs
- SafeThreadHandle.cs
- ColorMatrix.cs
- ConfigurationPropertyCollection.cs
- BindingMAnagerBase.cs
- GuidelineSet.cs
- TableSectionStyle.cs
- StructuralObject.cs
- VisualBasicSettingsHandler.cs
- ConnectivityStatus.cs
- TracedNativeMethods.cs
- WorkflowExecutor.cs