Example #1
0
        private IEnumerable <IEdmStructuralProperty> ReorderDependentProperties(
            NavigationPropertyConfiguration navProperty)
        {
            EntityTypeConfiguration principalEntity = _configurations.OfType <EntityTypeConfiguration>()
                                                      .FirstOrDefault(e => e.ClrType == navProperty.RelatedClrType);

            Contract.Assert(principalEntity != null);

            int keyCount = principalEntity.Keys().Count();

            if (keyCount != navProperty.PrincipalProperties.Count())
            {
                throw Error.InvalidOperation(SRResources.DependentPropertiesNotMatchWithPrincipalKeys,
                                             String.Join(",", navProperty.DependentProperties.Select(e => e.PropertyType.FullName)),
                                             String.Join(",", principalEntity.Keys.Select(e => e.PropertyInfo.PropertyType.FullName)));
            }

            // OData V3 spec says:
            // The property references for the principal entity MUST be the same property references specified
            // in the edm:Key of the principal entity type.
            IList <IEdmStructuralProperty> dependentProperties = new List <IEdmStructuralProperty>();

            foreach (PropertyConfiguration key in principalEntity.Keys())
            {
                PropertyInfo dependent =
                    navProperty.ReferentialConstraint.FirstOrDefault(r => r.Value.PropertyType == key.PropertyInfo.PropertyType &&
                                                                     r.Value.Name == key.PropertyInfo.Name).Key;

                if (dependent != null)
                {
                    dependentProperties.Add(_properties[dependent] as IEdmStructuralProperty);
                }
            }

            if (keyCount != dependentProperties.Count)
            {
                throw Error.InvalidOperation(SRResources.DependentPropertiesNotMatchWithPrincipalKeys,
                                             String.Join(",", navProperty.DependentProperties.Select(e => e.PropertyType.FullName)),
                                             String.Join(",", principalEntity.Keys.Select(e => e.PropertyInfo.PropertyType.FullName)));
            }

            return(dependentProperties);
        }