private static void PopulateDataTable <TEnity>(DataTable dataTable, ObjectContext objectContext, IList <TEnity> list, ReadOnlyMetadataCollection <EdmMember> storageSpaceMembers) where TEnity : class { Type typeOfEntity = typeof(TEnity); ReadOnlyMetadataCollection <EdmMember> objectSpaceMembers = objectContext.MetadataWorkspace.GetItem <EntityType>(typeOfEntity.FullName, true, DataSpace.OSpace).Members; EntitySetBase entitySetBase = objectContext.MetadataWorkspace.GetEntityContainer(objectContext.DefaultContainerName, DataSpace.CSpace).BaseEntitySets.FirstOrDefault(baseEntitySet => baseEntitySet.ElementType.Name.Equals(typeOfEntity.Name, StringComparison.OrdinalIgnoreCase)); EntityType entityType = (EntityType)entitySetBase.ElementType; foreach (TEnity entity in list) { DataRow dataRow = dataTable.NewRow(); foreach (EdmProperty edmProperty in entityType.Properties) { dataRow[storageSpaceMembers[objectSpaceMembers.IndexOf(objectSpaceMembers[edmProperty.Name])].Name] = (GetPropertyValue <object>(entity, edmProperty.Name) == null)?DBNull.Value: GetPropertyValue <object>(entity, edmProperty.Name); } if (entityType.NavigationProperties.Count > 0) { ICollection <ReferentialConstraint> referentialConstraints = GetReferentialConstraints(objectContext, typeOfEntity, entitySetBase); foreach (NavigationProperty navigationProperty in entityType.NavigationProperties) { IEnumerable <ReferentialConstraint> filteredReferentialConstraints = referentialConstraints.Where(referentialConstraint => referentialConstraint.FromRole.Name.Equals(navigationProperty.Name, StringComparison.OrdinalIgnoreCase)); foreach (ReferentialConstraint referentialConstraint in filteredReferentialConstraints) { dataRow[referentialConstraint.ToProperties[0].Name] = GetPropertyValue <object>(GetPropertyValue <object>(entity, referentialConstraint.FromRole.Name), referentialConstraint.FromProperties[0].Name); } } } dataTable.Rows.Add(dataRow); } }
//Return a set of integers that represent the indexes of first set of properties in the second set private Set <int> GetPropertyIndexes(IEnumerable <EdmProperty> properties1, ReadOnlyMetadataCollection <EdmProperty> properties2) { var propertyIndexes = new Set <int>(); foreach (var prop in properties1) { propertyIndexes.Add(properties2.IndexOf(prop)); } return(propertyIndexes); }
internal override int GetOrdinalforCLayerName(string name, StateManagerTypeMetadata metadata) { ReadOnlyMetadataCollection <AssociationEndMember> associationEndMembers = this._relationshipWrapper.AssociationEndMembers; AssociationEndMember associationEndMember; if (associationEndMembers.TryGetValue(name, false, out associationEndMember)) { return(associationEndMembers.IndexOf(associationEndMember)); } return(-1); }
private static Set <int> GetPropertyIndexes( IEnumerable <EdmProperty> properties1, ReadOnlyMetadataCollection <EdmProperty> properties2) { Set <int> set = new Set <int>(); foreach (EdmProperty edmProperty in properties1) { set.Add(properties2.IndexOf(edmProperty)); } return(set); }
/// <summary> /// Returns true if <paramref name="assocPath0"/> and <paramref name="assocPath1"/> are equivalent via a referential constraint in <paramref name="assocSet"/>. /// Requires: <paramref name="assocPath0"/> and <paramref name="assocPath1"/> correspond to paths in <paramref name="assocSet"/>. /// </summary> private static bool AreAssocationEndPathsEquivalentViaRefConstraint(MemberPath assocPath0, MemberPath assocPath1, AssociationSet assocSet) { Debug.Assert(assocPath0.Extent.Equals(assocSet) && assocPath1.Extent.Equals(assocSet), "Extent for paths must be assocSet"); AssociationEndMember end0 = assocPath0.RootEdmMember as AssociationEndMember; AssociationEndMember end1 = assocPath1.RootEdmMember as AssociationEndMember; EdmProperty property0 = assocPath0.LeafEdmMember as EdmProperty; EdmProperty property1 = assocPath1.LeafEdmMember as EdmProperty; if (end0 == null || end1 == null || property0 == null || property1 == null) { return(false); } // Now check if these fields are connected via a referential constraint AssociationType assocType = assocSet.ElementType; bool foundConstraint = false; foreach (ReferentialConstraint constraint in assocType.ReferentialConstraints) { bool isFrom0 = end0.Name == constraint.FromRole.Name && end1.Name == constraint.ToRole.Name; bool isFrom1 = end1.Name == constraint.FromRole.Name && end0.Name == constraint.ToRole.Name; if (isFrom0 || isFrom1) { // Found an RI for the two sets. Make sure that the properties are at the same ordinal // isFrom0 is true when end0 corresponds to FromRole and end1 to ToRole ReadOnlyMetadataCollection <EdmProperty> properties0 = isFrom0 ? constraint.FromProperties : constraint.ToProperties; ReadOnlyMetadataCollection <EdmProperty> properties1 = isFrom0 ? constraint.ToProperties : constraint.FromProperties; int indexForPath0 = properties0.IndexOf(property0); int indexForPath1 = properties1.IndexOf(property1); if (indexForPath0 == indexForPath1 && indexForPath0 != -1) { foundConstraint = true; break; } } } return(foundConstraint); }
/// <summary> /// Given a property on the principal end of a referential constraint, returns the corresponding property on the dependent end. /// Requires: The association has a referential constraint, and the specified principalProperty is one of the properties on the principal end. /// </summary> public EdmProperty GetCorrespondingDependentProperty(NavigationProperty navProperty, EdmProperty principalProperty) { if (navProperty == null) { throw new ArgumentNullException("navProperty"); } if (principalProperty == null) { throw new ArgumentNullException("principalProperty"); } ReadOnlyMetadataCollection <EdmProperty> fromProperties = GetPrincipalProperties(navProperty); ReadOnlyMetadataCollection <EdmProperty> toProperties = GetDependentProperties(navProperty); return(toProperties[fromProperties.IndexOf(principalProperty)]); }
private static bool AreAssocationEndPathsEquivalentViaRefConstraint( MemberPath assocPath0, MemberPath assocPath1, AssociationSet assocSet) { AssociationEndMember rootEdmMember1 = assocPath0.RootEdmMember as AssociationEndMember; AssociationEndMember rootEdmMember2 = assocPath1.RootEdmMember as AssociationEndMember; EdmProperty leafEdmMember1 = assocPath0.LeafEdmMember as EdmProperty; EdmProperty leafEdmMember2 = assocPath1.LeafEdmMember as EdmProperty; if (rootEdmMember1 == null || rootEdmMember2 == null || (leafEdmMember1 == null || leafEdmMember2 == null)) { return(false); } AssociationType elementType = assocSet.ElementType; bool flag1 = false; foreach (ReferentialConstraint referentialConstraint in elementType.ReferentialConstraints) { bool flag2 = rootEdmMember1.Name == referentialConstraint.FromRole.Name && rootEdmMember2.Name == referentialConstraint.ToRole.Name; bool flag3 = rootEdmMember2.Name == referentialConstraint.FromRole.Name && rootEdmMember1.Name == referentialConstraint.ToRole.Name; if (flag2 || flag3) { ReadOnlyMetadataCollection <EdmProperty> metadataCollection1 = flag2 ? referentialConstraint.FromProperties : referentialConstraint.ToProperties; ReadOnlyMetadataCollection <EdmProperty> metadataCollection2 = flag2 ? referentialConstraint.ToProperties : referentialConstraint.FromProperties; int num1 = metadataCollection1.IndexOf(leafEdmMember1); int num2 = metadataCollection2.IndexOf(leafEdmMember2); if (num1 == num2 && num1 != -1) { flag1 = true; break; } } } return(flag1); }
//Return a set of integers that represent the indexes of first set of properties in the second set private static Set<int> GetPropertyIndexes( IEnumerable<EdmProperty> properties1, ReadOnlyMetadataCollection<EdmProperty> properties2) { var propertyIndexes = new Set<int>(); foreach (var prop in properties1) { propertyIndexes.Add(properties2.IndexOf(prop)); } return propertyIndexes; }