/// <summary> /// Compare EntitySet elements from CSDL against those in an Edm model. /// </summary> /// <param name="containerElements">The EntityContainer elements containing the EntitySet elements.</param> /// <param name="container">The EdmContainer from the model to compare against.</param> /// <param name="entityTypes">All entity types from the model, for verifying navigation property bindings.</param> private static void CompareEntitySets(IEnumerable <XElement> containerElements, IEdmEntityContainer container, IEnumerable <IEdmEntityType> entityTypes) { var entitySetElements = containerElements.SelectMany(e => e.EdmElements("EntitySet")).ToArray(); var entitySets = container.EntitySets().ToArray(); ExceptionUtilities.Assert(entitySetElements.Count() == entitySets.Count(), "Unexpected number of entity sets"); foreach (var entitySetElement in entitySetElements) { var entitySetName = entitySetElement.GetAttributeValue("Name"); var entitySet = entitySets.SingleOrDefault(e => e.Name == entitySetName); ExceptionUtilities.Assert(entitySet != null, "Failed to find entity set " + entitySetName); ExceptionUtilities.Assert(entitySetElement.GetAttributeValue("EntityType") == entitySet.EntityType().FullName(), "Unexpected type for entity set " + entitySetName); // Compare Navigation Property Bindings var navigationPropertyBindingElements = entitySetElement.EdmElements("NavigationPropertyBinding").ToArray(); ExceptionUtilities.Assert(navigationPropertyBindingElements.Count() == entitySet.NavigationPropertyBindings.Count(), "Unexpected number of navigation targets"); foreach (var navigationPropertyBindingElement in navigationPropertyBindingElements) { var pathValue = navigationPropertyBindingElement.GetAttributeValue("Path"); var targetValue = navigationPropertyBindingElement.GetAttributeValue("Target"); string propertyName = pathValue; if (pathValue.Contains("/")) { // Path contains a derived type - split it to determine the property name and verify the // type is derived from the entity set type, in the model. var splitPath = pathValue.Split(new[] { '/' }, StringSplitOptions.None); ExceptionUtilities.Assert(2 == splitPath.Count(), "More than two forward slash characters in nav property binding path"); var derivedTypeName = splitPath.ElementAt(0); propertyName = splitPath.ElementAt(1); var derivedType = entityTypes.SingleOrDefault(t => t.FullName() == splitPath.ElementAt(0)); ExceptionUtilities.Assert(derivedType != null, "Failed to find entity type " + derivedTypeName + " described in navigation property binding path"); ExceptionUtilities.Assert(derivedType.DeclaredNavigationProperties().SingleOrDefault(p => p.Name == propertyName) != null, "Failed to find nav property on derived type"); EdmModelUtils.AssertEntityTypeIsDerivedFrom(derivedType, entitySet.EntityType()); } var navigationTarget = entitySet.NavigationPropertyBindings.SingleOrDefault(nt => nt.NavigationProperty.Name == propertyName && nt.Target.Name == targetValue); ExceptionUtilities.Assert(navigationTarget != null, "Failed to find navigation target for element " + navigationPropertyBindingElement); } } }
/// <summary> /// Compares the type name value against the model type reference, taking into account collection /// types and qualified/non-qualified type names. /// </summary> /// <param name="typeName">The CSDL type value.</param> /// <param name="typeReference">The Edm model type reference to compare against.</param> private static void CompareTypeValue(string typeName, IEdmTypeReference typeReference) { bool isCollectionType = typeName.StartsWith(EdmModelUtils.CollectionTypeNamePrefix); if (isCollectionType) { ExceptionUtilities.Assert(typeReference.IsCollection(), "Expected collection type"); string elementTypeName = EdmModelUtils.GetCollectionItemTypeName(typeName); CompareTypeValue(elementTypeName, typeReference.GetCollectionItemType()); } else { bool expectedTypeNameContainsNamespace = typeName.Contains("."); if (expectedTypeNameContainsNamespace) { ExceptionUtilities.Assert(typeName == typeReference.FullName(), "Unexpected type"); } else { ExceptionUtilities.Assert("Edm." + typeName == typeReference.FullName(), "Unexpected type"); } } }