/// <summary>
        ///     Load the property with scalar property attribute.
        ///     Note that we pass the CLR type in because in the case where the property is declared on a generic
        ///     base class the DeclaringType of propert won't work for us and we need the real entity type instead.
        /// </summary>
        /// <param name="clrType"> The CLR type of the entity </param>
        /// <param name="property"> Metadata representing the property </param>
        /// <param name="isEntityKeyProperty"> True if the property forms part of the entity's key </param>
        private EdmMember LoadScalarProperty(Type clrType, PropertyInfo property, out bool isEntityKeyProperty)
        {
            Debug.Assert(property.IsDefined(typeof(EdmScalarPropertyAttribute), false), "The property must have a scalar attribute");
            EdmMember member = null;

            isEntityKeyProperty = false;

            // Load the property type and create a new property object
            PrimitiveType primitiveType;

            // If the type could not be loaded it's definitely not a primitive type, so that's an error
            // If it could be loaded but is not a primitive that's an error as well
            if (!TryGetPrimitiveType(property.PropertyType, out primitiveType))
            {
                // This property does not need to be validated further, just add to the errors collection and continue with the next property
                // This failure will cause an exception to be thrown later during validation of all of the types
                SessionData.EdmItemErrors.Add(
                    new EdmItemError(
                        Strings.Validator_OSpace_ScalarPropertyNotPrimitive(
                            property.Name, property.DeclaringType.FullName, property.PropertyType.FullName)));
            }
            else
            {
                var attrs = property.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false);

                Debug.Assert(attrs.Length == 1, "Every property can exactly have one ScalarProperty Attribute");
                // Expecting EdmScalarPropertyAttribute to have AllowMultiple=False, so only look at first element in the attribute array
                isEntityKeyProperty = ((EdmScalarPropertyAttribute)attrs[0]).EntityKeyProperty;
                var isNullable = ((EdmScalarPropertyAttribute)attrs[0]).IsNullable;

                member = new EdmProperty(
                    property.Name,
                    TypeUsage.Create(
                        primitiveType, new FacetValues
                {
                    Nullable = isNullable
                }),
                    property, clrType);
            }
            return(member);
        }
        public void EdmFunction_DuplicateParameterName()
        {
            var validationContext
                = new EdmModelValidationContext(new EdmModel(DataSpace.SSpace), true);

            DataModelErrorEventArgs errorEventArgs = null;

            validationContext.OnError += (_, e) => errorEventArgs = e;

            var parameter1
                = new FunctionParameter(
                      "P",
                      TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                      ParameterMode.In);

            var parameter2
                = new FunctionParameter(
                      "P2",
                      TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)),
                      ParameterMode.In);

            var function
                = new EdmFunction(
                      "F", "N", DataSpace.SSpace,
                      new EdmFunctionPayload
            {
                Parameters = new[] { parameter1, parameter2 }
            });

            parameter2.Name = "P";

            EdmModelSemanticValidationRules
            .EdmFunction_DuplicateParameterName
            .Evaluate(validationContext, function);

            Assert.NotNull(errorEventArgs);
            Assert.Same(parameter2, errorEventArgs.Item);
            Assert.Equal(
                Strings.ParameterNameAlreadyDefinedDuplicate("P"),
                errorEventArgs.ErrorMessage);
        }
Esempio n. 3
0
        // <summary>
        // Validates whether cspace and sspace types are compatible.
        // </summary>
        // <param name="cspaceType"> Type in C-Space. Must be a primitive or enumeration type. </param>
        // <param name="storeType"> C-Space equivalent of S-space Type. Must be a primitive type. </param>
        // <returns>
        // <c>true</c> if the types are compatible. <c>false</c> otherwise.
        // </returns>
        // <remarks>
        // This methods validate whether cspace and sspace types are compatible. The types are
        // compatible if:
        // both are primitive and the cspace type is a subtype of sspace type
        // or
        // cspace type is an enumeration type whose underlying type is a subtype of sspace type.
        // </remarks>
        private static bool ValidateScalarTypesAreCompatible(TypeUsage cspaceType, TypeUsage storeType)
        {
            DebugCheck.NotNull(cspaceType);
            DebugCheck.NotNull(storeType);
            Debug.Assert(cspaceType.EdmType.DataSpace == DataSpace.CSpace, "cspace property must have a cspace type");
            Debug.Assert(storeType.EdmType.DataSpace == DataSpace.CSpace, "storeType type usage must have a sspace type");
            Debug.Assert(
                IsScalarType(cspaceType.EdmType),
                "cspace property must be of a primitive or enumeration type");
            Debug.Assert(IsPrimitiveType(storeType.EdmType), "storeType property must be a primitive type");

            if (IsEnumType(cspaceType.EdmType))
            {
                // For enum cspace type check whether its underlying type is a subtype of the store type. Note that
                // TypeSemantics.IsSubTypeOf uses only TypeUsage.EdmType for primitive types so there is no need to copy facets
                // from the enum type property to the underlying type TypeUsage created here since they wouldn't be used anyways.
                return(TypeSemantics.IsSubTypeOf(TypeUsage.Create(GetUnderlyingEdmTypeForEnumType(cspaceType.EdmType)), storeType));
            }

            return(TypeSemantics.IsSubTypeOf(cspaceType, storeType));
        }
        internal EdmProviderManifestFunctionBuilder(ReadOnlyCollection <PrimitiveType> edmPrimitiveTypes)
        {
            Debug.Assert(edmPrimitiveTypes != null, "Primitive types should not be null");

            // Initialize all the various parameter types. We do not want to create new instance of parameter types
            // again and again for perf reasons
            var primitiveTypeUsages = new TypeUsage[edmPrimitiveTypes.Count];

            foreach (var edmType in edmPrimitiveTypes)
            {
                Debug.Assert(
                    (int)edmType.PrimitiveTypeKind < primitiveTypeUsages.Length && (int)edmType.PrimitiveTypeKind >= 0,
                    "Invalid PrimitiveTypeKind value?");
                Debug.Assert(
                    primitiveTypeUsages[(int)edmType.PrimitiveTypeKind] == null, "Duplicate PrimitiveTypeKind value in EDM primitive types?");

                primitiveTypeUsages[(int)edmType.PrimitiveTypeKind] = TypeUsage.Create(edmType);
            }

            primitiveTypes = primitiveTypeUsages;
        }
Esempio n. 5
0
        private static void AddScalarMember(
            Type type, PropertyInfo clrProperty, StructuralType ospaceType, EdmProperty cspaceProperty, EdmType propertyType)
        {
            DebugCheck.NotNull(type);
            DebugCheck.NotNull(clrProperty);
            Debug.Assert(clrProperty.CanRead && clrProperty.CanWriteExtended(), "The clr property has to have a setter and a getter.");
            DebugCheck.NotNull(ospaceType);
            DebugCheck.NotNull(cspaceProperty);
            DebugCheck.NotNull(propertyType);
            Debug.Assert(Helper.IsScalarType(propertyType), "Property has to be primitive or enum.");

            var cspaceType = cspaceProperty.DeclaringType;

            var isKeyMember = Helper.IsEntityType(cspaceType) && ((EntityType)cspaceType).KeyMemberNames.Contains(clrProperty.Name);

            // the property is nullable only if it is not a key and can actually be set to null (i.e. is not a value type or is a nullable value type)
            var nullableFacetValue = !isKeyMember
                                     &&
                                     (!clrProperty.PropertyType.IsValueType || Nullable.GetUnderlyingType(clrProperty.PropertyType) != null);

            var ospaceProperty =
                new EdmProperty(
                    cspaceProperty.Name,
                    TypeUsage.Create(
                        propertyType, new FacetValues
            {
                Nullable = nullableFacetValue
            }),
                    clrProperty,
                    type);

            if (isKeyMember)
            {
                ((EntityType)ospaceType).AddKeyMember(ospaceProperty);
            }
            else
            {
                ospaceType.AddMember(ospaceProperty);
            }
        }
Esempio n. 6
0
        public void NavigationProperties_is_thread_safe()
        {
            var entityType = new EntityType("E", "N", DataSpace.CSpace);

            var property = new NavigationProperty("N", TypeUsage.Create(new EntityType("E", "N", DataSpace.CSpace)));

            entityType.AddMember(property);

            const int cycles      = 100;
            const int threadCount = 30;

            Action readNavigationProperties = () =>
            {
                for (var i = 0; i < cycles; ++i)
                {
                    var navProps = entityType.NavigationProperties;

                    //touching Members.Source triggers a reset to NavigationProperties
                    var sourceCount = entityType.Members.Source.Count;
                    Assert.True(sourceCount == 1);

                    var navigationPropertiesAfterReset = entityType.NavigationProperties;

                    Assert.True(navProps != null, "First reference to NavigationProperties should not be null");
                    Assert.True(navigationPropertiesAfterReset != null, "Second reference to NavigationProperties should not be null");
                    Assert.False(ReferenceEquals(navProps, navigationPropertiesAfterReset), "The NavigationProperties instances should be different");
                }
            };

            var tasks = new List <Thread>();

            for (var i = 0; i < threadCount; ++i)
            {
                tasks.Add(new Thread(new ThreadStart(readNavigationProperties)));
            }

            tasks.ForEach(t => t.Start());
            tasks.ForEach(t => t.Join());
        }
Esempio n. 7
0
        public void Can_get_list_of_declared_navigation_properties()
        {
            var entityType = new EntityType();

            Assert.Empty(entityType.DeclaredNavigationProperties);

            var property = new NavigationProperty("N", TypeUsage.Create(new EntityType()));

            entityType.AddMember(property);

            Assert.Equal(1, entityType.DeclaredNavigationProperties.Count);

            entityType.RemoveMember(property);

            var baseType = new EntityType();

            baseType.AddMember(property);

            entityType.BaseType = baseType;

            Assert.Empty(entityType.DeclaredNavigationProperties);
            Assert.Equal(1, entityType.Members.Count);
        }
Esempio n. 8
0
        private void CreateAndAddComplexType(Type type, StructuralType ospaceType, EdmProperty cspaceProperty, PropertyInfo clrProperty)
        {
            EdmType propertyType;

            if (CspaceToOspace.TryGetValue(cspaceProperty.TypeUsage.EdmType, out propertyType))
            {
                Debug.Assert(propertyType is StructuralType, "Structural type expected.");

                var property = new EdmProperty(
                    cspaceProperty.Name, TypeUsage.Create(
                        propertyType, new FacetValues
                {
                    Nullable = false
                }), clrProperty, type);
                ospaceType.AddMember(property);
            }
            else
            {
                LogError(
                    Strings.Validator_OSpace_Convention_MissingOSpaceType(cspaceProperty.TypeUsage.EdmType.FullName),
                    cspaceProperty.TypeUsage.EdmType);
            }
        }
Esempio n. 9
0
        private void ResolveEnumTypeProperty(StructuralType declaringType, PropertyInfo clrProperty)
        {
            EdmType edmType;

            if (!this.TryGetLoadedType(clrProperty.PropertyType, out edmType) || !Helper.IsEnumType(edmType))
            {
                this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_ScalarPropertyNotPrimitive((object)clrProperty.Name, (object)clrProperty.DeclaringType.FullName, (object)clrProperty.PropertyType.FullName)));
            }
            else
            {
                EdmScalarPropertyAttribute propertyAttribute = clrProperty.GetCustomAttributes <EdmScalarPropertyAttribute>(false).Single <EdmScalarPropertyAttribute>();
                EdmProperty edmProperty = new EdmProperty(clrProperty.Name, TypeUsage.Create(edmType, new FacetValues()
                {
                    Nullable = (FacetValueContainer <bool?>) new bool?(propertyAttribute.IsNullable)
                }), clrProperty, declaringType.ClrType);
                declaringType.AddMember((EdmMember)edmProperty);
                if (declaringType.BuiltInTypeKind != BuiltInTypeKind.EntityType || !propertyAttribute.EntityKeyProperty)
                {
                    return;
                }
                ((EntityTypeBase)declaringType).AddKeyMember((EdmMember)edmProperty);
            }
        }
Esempio n. 10
0
        public void Cannot_create_property_of_invalid_type()
        {
            var rowType =
                RowType.Create(
                    new[]
            {
                EdmProperty.Primitive("property", PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32))
            },
                    null);

            Assert.Equal(
                Strings.EdmProperty_InvalidPropertyType(rowType.FullName),
                Assert.Throws <ArgumentException>(() => EdmProperty.Create("invalidProperty", TypeUsage.Create(rowType))).Message);
        }
Esempio n. 11
0
 /// <summary>
 /// Creates a <see cref="T:System.Data.Entity.Core.Metadata.Edm.TypeUsage" /> object with the specified conceptual model type.
 /// </summary>
 /// <returns>
 /// A <see cref="T:System.Data.Entity.Core.Metadata.Edm.TypeUsage" /> object with the default facet values for the specified
 /// <see cref="T:System.Data.Entity.Core.Metadata.Edm.EdmType" />
 /// .
 /// </returns>
 /// <param name="edmType">
 /// A <see cref="T:System.Data.Entity.Core.Metadata.Edm.EdmType" /> for which the
 /// <see cref="T:System.Data.Entity.Core.Metadata.Edm.TypeUsage" />
 /// object is created.
 /// </param>
 public static TypeUsage CreateDefaultTypeUsage(EdmType edmType)
 {
     Check.NotNull <EdmType>(edmType, nameof(edmType));
     return(TypeUsage.Create(edmType));
 }
Esempio n. 12
0
 // <summary>
 // determines if two types are equivalent or if fromType is promotable to toType
 // </summary>
 // <returns> true if fromType equivalent or promotable to toType, false otherwise </returns>
 internal static bool IsStructurallyEqualOrPromotableTo(EdmType fromType, EdmType toType)
 {
     return(IsStructurallyEqualOrPromotableTo(TypeUsage.Create(fromType), TypeUsage.Create(toType)));
 }
Esempio n. 13
0
 // <summary>
 // determines if fromEdmType can be cast to toEdmType. this operation is valid only
 // if fromtype and totype are polimorphic types.
 // </summary>
 internal static bool IsValidPolymorphicCast(EdmType fromEdmType, EdmType toEdmType)
 {
     return(IsValidPolymorphicCast(TypeUsage.Create(fromEdmType), TypeUsage.Create(toEdmType)));
 }
Esempio n. 14
0
 internal TypeUsage ShallowCopy(params Facet[] facetValues)
 {
     return(TypeUsage.Create(this._edmType, TypeUsage.OverrideFacetValues((IEnumerable <Facet>) this.Facets, (IEnumerable <Facet>)facetValues)));
 }
Esempio n. 15
0
        public void WriteAssociationSetMapping_should_write_modification_function_mapping()
        {
            var fixture = new Fixture();

            var entityType = new EntityType("E", "N", DataSpace.CSpace);
            var entitySet  = new EntitySet("ES", "S", null, null, entityType);

            new EntityContainer("EC", DataSpace.SSpace).AddEntitySetBase(entitySet);
            var associationSet = new AssociationSet("AS", new AssociationType("A", XmlConstants.ModelNamespace_3, false, DataSpace.CSpace));

            var associationEndMember1 = new AssociationEndMember("Source", new EntityType("E", "N", DataSpace.CSpace));

            associationSet.AddAssociationSetEnd(new AssociationSetEnd(entitySet, associationSet, associationEndMember1));

            var associationEndMember2 = new AssociationEndMember("Target", new EntityType("E", "N", DataSpace.CSpace));

            associationSet.AddAssociationSetEnd(new AssociationSetEnd(entitySet, associationSet, associationEndMember2));

            var storageModificationFunctionMapping
                = new ModificationFunctionMapping(
                      associationSet,
                      associationSet.ElementType,
                      new EdmFunction("F", "N", DataSpace.SSpace, new EdmFunctionPayload()),
                      new[]
            {
                new ModificationFunctionParameterBinding(
                    new FunctionParameter(
                        "P",
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                        ParameterMode.In),
                    new ModificationFunctionMemberPath(
                        new EdmMember[]
                {
                    new EdmProperty("K"),
                    associationEndMember1
                },
                        associationSet),
                    true),
                new ModificationFunctionParameterBinding(
                    new FunctionParameter(
                        "P",
                        TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.Int32)),
                        ParameterMode.In),
                    new ModificationFunctionMemberPath(
                        new EdmMember[]
                {
                    new EdmProperty("K"),
                    associationEndMember2
                },
                        associationSet),
                    false)
            },
                      null,
                      null);

            var associationSetMapping
                = new AssociationSetMapping(
                      associationSet,
                      entitySet)
                {
                SourceEndMapping
                    = new EndPropertyMapping
                    {
                    AssociationEnd = associationEndMember1
                    },
                TargetEndMapping
                    = new EndPropertyMapping
                    {
                    AssociationEnd = associationEndMember2
                    },
                ModificationFunctionMapping = new AssociationSetModificationFunctionMapping(
                    associationSet,
                    storageModificationFunctionMapping,
                    storageModificationFunctionMapping)
                };

            fixture.Writer.WriteAssociationSetMappingElement(associationSetMapping);

            Assert.Equal(
                @"<AssociationSetMapping Name=""AS"" TypeName="".A"" StoreEntitySet=""E"">
  <EndProperty Name=""Source"" />
  <EndProperty Name=""Target"" />
  <ModificationFunctionMapping>
    <InsertFunction FunctionName=""N.F"">
      <EndProperty Name=""Source"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Current"" />
      </EndProperty>
      <EndProperty Name=""Target"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Original"" />
      </EndProperty>
    </InsertFunction>
    <DeleteFunction FunctionName=""N.F"">
      <EndProperty Name=""Source"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Current"" />
      </EndProperty>
      <EndProperty Name=""Target"">
        <ScalarProperty Name=""K"" ParameterName=""P"" Version=""Original"" />
      </EndProperty>
    </DeleteFunction>
  </ModificationFunctionMapping>
</AssociationSetMapping>",
                fixture.ToString());
        }
        static MetadataItem()
        {
            MetadataItem._builtInTypes[0]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[2]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[1]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[3]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[3]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[7]  = (EdmType) new EnumType();
            MetadataItem._builtInTypes[6]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[8]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[9]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[10] = (EdmType) new EnumType();
            MetadataItem._builtInTypes[11] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[12] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[13] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[14] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[4]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[5]  = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[15] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[16] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[17] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[18] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[19] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[20] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[21] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[22] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[23] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[24] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[25] = (EdmType) new EnumType();
            MetadataItem._builtInTypes[26] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[27] = (EdmType) new EnumType();
            MetadataItem._builtInTypes[28] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[29] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[30] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[31] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[32] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[33] = (EdmType) new EnumType();
            MetadataItem._builtInTypes[34] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[35] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[36] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[37] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[38] = (EdmType) new ComplexType();
            MetadataItem._builtInTypes[39] = (EdmType) new ComplexType();
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem), "ItemType", false, (ComplexType)null);
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataProperty), "MetadataProperty", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.GlobalItem), "GlobalItem", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.TypeUsage), "TypeUsage", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType), "EdmType", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.GlobalItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.SimpleType), "SimpleType", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EnumType), "EnumType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.SimpleType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.PrimitiveType), "PrimitiveType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.SimpleType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.CollectionType), "CollectionType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RefType), "RefType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmMember), "EdmMember", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmProperty), "EdmProperty", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmMember));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.NavigationProperty), "NavigationProperty", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmMember));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.ProviderManifest), "ProviderManifest", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipEndMember), "RelationshipEnd", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmMember));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.AssociationEndMember), "AssociationEnd", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipEndMember));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EnumMember), "EnumMember", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.ReferentialConstraint), "ReferentialConstraint", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.StructuralType), "StructuralType", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RowType), "RowType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.StructuralType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.ComplexType), "ComplexType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.StructuralType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntityTypeBase), "ElementType", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.StructuralType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntityType), "EntityType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntityTypeBase));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipType), "RelationshipType", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntityTypeBase));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.AssociationType), "AssociationType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.Facet), "Facet", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntityContainer), "EntityContainerType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.GlobalItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntitySetBase), "BaseEntitySetType", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntitySet), "EntitySetType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntitySetBase));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipSet), "RelationshipSet", true, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EntitySetBase));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.AssociationSet), "AssocationSetType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipSet));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.AssociationSetEnd), "AssociationSetEndType", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.FunctionParameter), "FunctionParameter", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmFunction), "EdmFunction", false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType));
            MetadataItem.InitializeBuiltInTypes((ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.Documentation), nameof(Documentation), false, (ComplexType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataItem));
            MetadataItem.InitializeEnumType(BuiltInTypeKind.OperationAction, "DeleteAction", new string[2]
            {
                "None",
                "Cascade"
            });
            MetadataItem.InitializeEnumType(BuiltInTypeKind.RelationshipMultiplicity, "RelationshipMultiplicity", new string[3]
            {
                "One",
                "ZeroToOne",
                "Many"
            });
            MetadataItem.InitializeEnumType(BuiltInTypeKind.ParameterMode, "ParameterMode", new string[3]
            {
                "In",
                "Out",
                "InOut"
            });
            MetadataItem.InitializeEnumType(BuiltInTypeKind.CollectionKind, "CollectionKind", new string[3]
            {
                "None",
                "List",
                "Bag"
            });
            MetadataItem.InitializeEnumType(BuiltInTypeKind.PrimitiveTypeKind, "PrimitiveTypeKind", Enum.GetNames(typeof(PrimitiveTypeKind)));
            FacetDescription[] facetDescriptionArray = new FacetDescription[2];
            MetadataItem._nullableFacetDescription = new FacetDescription("Nullable", (EdmType)MetadataItem.EdmProviderManifest.GetPrimitiveType(PrimitiveTypeKind.Boolean), new int?(), new int?(), (object)true);
            facetDescriptionArray[0] = MetadataItem._nullableFacetDescription;
            MetadataItem._defaultValueFacetDescription = new FacetDescription("DefaultValue", MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType), new int?(), new int?(), (object)null);
            facetDescriptionArray[1] = MetadataItem._defaultValueFacetDescription;
            MetadataItem._generalFacetDescriptions       = new ReadOnlyCollection <FacetDescription>((IList <FacetDescription>)facetDescriptionArray);
            MetadataItem._collectionKindFacetDescription = new FacetDescription("CollectionKind", MetadataItem.GetBuiltInType(BuiltInTypeKind.EnumType), new int?(), new int?(), (object)null);
            TypeUsage typeUsage1 = TypeUsage.Create((EdmType)MetadataItem.EdmProviderManifest.GetPrimitiveType(PrimitiveTypeKind.String));
            TypeUsage typeUsage2 = TypeUsage.Create((EdmType)MetadataItem.EdmProviderManifest.GetPrimitiveType(PrimitiveTypeKind.Boolean));
            TypeUsage typeUsage3 = TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType));
            TypeUsage typeUsage4 = TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.TypeUsage));
            TypeUsage typeUsage5 = TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.ComplexType));

            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.MetadataProperty, new EdmProperty[3]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("TypeUsage", typeUsage4),
                new EdmProperty("Value", typeUsage5)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.MetadataItem, new EdmProperty[2]
            {
                new EdmProperty(nameof(MetadataProperties), TypeUsage.Create((EdmType)MetadataItem.GetBuiltInType(BuiltInTypeKind.MetadataProperty).GetCollectionType())),
                new EdmProperty(nameof(Documentation), TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.Documentation)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.TypeUsage, new EdmProperty[2]
            {
                new EdmProperty("EdmType", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType))),
                new EdmProperty("Facets", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.Facet)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EdmType, new EdmProperty[5]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("Namespace", typeUsage1),
                new EdmProperty("Abstract", typeUsage2),
                new EdmProperty("Sealed", typeUsage2),
                new EdmProperty("BaseType", typeUsage5)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EnumType, new EdmProperty[1]
            {
                new EdmProperty("EnumMembers", typeUsage1)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.CollectionType, new EdmProperty[1]
            {
                new EdmProperty("TypeUsage", typeUsage4)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.RefType, new EdmProperty[1]
            {
                new EdmProperty("EntityType", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EntityType)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EdmMember, new EdmProperty[2]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("TypeUsage", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.TypeUsage)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EdmProperty, new EdmProperty[2]
            {
                new EdmProperty("Nullable", typeUsage1),
                new EdmProperty("DefaultValue", typeUsage5)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.NavigationProperty, new EdmProperty[2]
            {
                new EdmProperty("RelationshipTypeName", typeUsage1),
                new EdmProperty("ToEndMemberName", typeUsage1)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.RelationshipEndMember, new EdmProperty[2]
            {
                new EdmProperty("OperationBehaviors", typeUsage5),
                new EdmProperty("RelationshipMultiplicity", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EnumType)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EnumMember, new EdmProperty[1]
            {
                new EdmProperty("Name", typeUsage1)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.ReferentialConstraint, new EdmProperty[4]
            {
                new EdmProperty("ToRole", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipEndMember))),
                new EdmProperty("FromRole", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.RelationshipEndMember))),
                new EdmProperty("ToProperties", TypeUsage.Create((EdmType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmProperty).GetCollectionType())),
                new EdmProperty("FromProperties", TypeUsage.Create((EdmType)MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmProperty).GetCollectionType()))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.StructuralType, new EdmProperty[1]
            {
                new EdmProperty("Members", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmMember)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EntityTypeBase, new EdmProperty[1]
            {
                new EdmProperty("KeyMembers", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmMember)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.Facet, new EdmProperty[3]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("EdmType", typeUsage3),
                new EdmProperty("Value", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EdmType)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EntityContainer, new EdmProperty[2]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("EntitySets", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EntitySet)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EntitySetBase, new EdmProperty[4]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("EntityType", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EntityType))),
                new EdmProperty("Schema", typeUsage1),
                new EdmProperty("Table", typeUsage1)
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.AssociationSet, new EdmProperty[1]
            {
                new EdmProperty("AssociationSetEnds", TypeUsage.Create((EdmType)MetadataItem.GetBuiltInType(BuiltInTypeKind.AssociationSetEnd).GetCollectionType()))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.AssociationSetEnd, new EdmProperty[2]
            {
                new EdmProperty("Role", typeUsage1),
                new EdmProperty("EntitySetType", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EntitySet)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.FunctionParameter, new EdmProperty[3]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("Mode", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.EnumType))),
                new EdmProperty("TypeUsage", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.TypeUsage)))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.EdmFunction, new EdmProperty[4]
            {
                new EdmProperty("Name", typeUsage1),
                new EdmProperty("Namespace", typeUsage1),
                new EdmProperty("ReturnParameter", TypeUsage.Create(MetadataItem.GetBuiltInType(BuiltInTypeKind.FunctionParameter))),
                new EdmProperty("Parameters", TypeUsage.Create((EdmType)MetadataItem.GetBuiltInType(BuiltInTypeKind.FunctionParameter).GetCollectionType()))
            });
            MetadataItem.AddBuiltInTypeProperties(BuiltInTypeKind.Documentation, new EdmProperty[2]
            {
                new EdmProperty("Summary", typeUsage1),
                new EdmProperty("LongDescription", typeUsage1)
            });
            for (int index = 0; index < MetadataItem._builtInTypes.Length; ++index)
            {
                MetadataItem._builtInTypes[index].SetReadOnly();
            }
        }
        static MetadataItem()
        {
            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Bootstrapping the builtin types
            ////////////////////////////////////////////////////////////////////////////////////////////////
            _builtInTypes[(int)BuiltInTypeKind.AssociationEndMember] = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.AssociationSet]       = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.AssociationSetEnd]    = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.AssociationType]      = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.AssociationType]      = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.CollectionKind]       = new EnumType();
            _builtInTypes[(int)BuiltInTypeKind.CollectionType]       = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.ComplexType]          = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.Documentation]        = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.OperationAction]      = new EnumType();
            _builtInTypes[(int)BuiltInTypeKind.EdmType]                  = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EntityContainer]          = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EntitySet]                = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EntityType]               = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EntitySetBase]            = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EntityTypeBase]           = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EnumType]                 = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EnumMember]               = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.Facet]                    = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EdmFunction]              = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.FunctionParameter]        = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.GlobalItem]               = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.MetadataProperty]         = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.NavigationProperty]       = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.MetadataItem]             = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.EdmMember]                = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.ParameterMode]            = new EnumType();
            _builtInTypes[(int)BuiltInTypeKind.PrimitiveType]            = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.PrimitiveTypeKind]        = new EnumType();
            _builtInTypes[(int)BuiltInTypeKind.EdmProperty]              = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.ProviderManifest]         = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.ReferentialConstraint]    = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.RefType]                  = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.RelationshipEndMember]    = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.RelationshipMultiplicity] = new EnumType();
            _builtInTypes[(int)BuiltInTypeKind.RelationshipSet]          = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.RelationshipType]         = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.RowType]                  = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.SimpleType]               = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.StructuralType]           = new ComplexType();
            _builtInTypes[(int)BuiltInTypeKind.TypeUsage]                = new ComplexType();

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Initialize item attributes for all the built-in complex types
            ////////////////////////////////////////////////////////////////////////////////////////////////

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem),
                EdmConstants.ItemType,
                false /*isAbstract*/,
                null);

            // populate the attributes for item attributes
            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataProperty),
                EdmConstants.ItemAttribute,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.GlobalItem),
                EdmConstants.GlobalItem,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.TypeUsage),
                EdmConstants.TypeUsage,
                false, /*isAbstract*/
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            //populate the attributes for the edm type
            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmType),
                EdmConstants.EdmType,
                true, /*isAbstract*/
                (ComplexType)GetBuiltInType(BuiltInTypeKind.GlobalItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.SimpleType),
                EdmConstants.SimpleType,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EnumType),
                EdmConstants.EnumerationType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.SimpleType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.PrimitiveType),
                EdmConstants.PrimitiveType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.SimpleType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.CollectionType),
                EdmConstants.CollectionType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RefType),
                EdmConstants.RefType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmMember),
                EdmConstants.Member,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmProperty),
                EdmConstants.Property,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmMember));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.NavigationProperty),
                EdmConstants.NavigationProperty,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmMember));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.ProviderManifest),
                EdmConstants.ProviderManifest,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RelationshipEndMember),
                EdmConstants.RelationshipEnd,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmMember));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.AssociationEndMember),
                EdmConstants.AssociationEnd,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RelationshipEndMember));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EnumMember),
                EdmConstants.EnumerationMember,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.ReferentialConstraint),
                EdmConstants.ReferentialConstraint,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            // Structural Type hierarchy
            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.StructuralType),
                EdmConstants.StructuralType,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RowType),
                EdmConstants.RowType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.StructuralType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.ComplexType),
                EdmConstants.ComplexType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.StructuralType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntityTypeBase),
                EdmConstants.ElementType,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.StructuralType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntityType),
                EdmConstants.EntityType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntityTypeBase));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RelationshipType),
                EdmConstants.RelationshipType,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntityTypeBase));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.AssociationType),
                EdmConstants.AssociationType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RelationshipType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.Facet),
                EdmConstants.Facet,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntityContainer),
                EdmConstants.EntityContainerType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.GlobalItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntitySetBase),
                EdmConstants.BaseEntitySetType,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntitySet),
                EdmConstants.EntitySetType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntitySetBase));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RelationshipSet),
                EdmConstants.RelationshipSet,
                true /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EntitySetBase));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.AssociationSet),
                EdmConstants.AssociationSetType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.RelationshipSet));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.AssociationSetEnd),
                EdmConstants.AssociationSetEndType,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.FunctionParameter),
                EdmConstants.FunctionParameter,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmFunction),
                EdmConstants.Function,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.EdmType));

            InitializeBuiltInTypes(
                (ComplexType)GetBuiltInType(BuiltInTypeKind.Documentation),
                EdmConstants.Documentation,
                false /*isAbstract*/,
                (ComplexType)GetBuiltInType(BuiltInTypeKind.MetadataItem));

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Initialize item attributes for all the built-in enum types
            ////////////////////////////////////////////////////////////////////////////////////////////////
            InitializeEnumType(
                BuiltInTypeKind.OperationAction,
                EdmConstants.DeleteAction,
                new[] { EdmConstants.None, EdmConstants.Cascade });

            InitializeEnumType(
                BuiltInTypeKind.RelationshipMultiplicity,
                EdmConstants.RelationshipMultiplicity,
                new[] { EdmConstants.One, EdmConstants.ZeroToOne, EdmConstants.Many });

            InitializeEnumType(
                BuiltInTypeKind.ParameterMode,
                EdmConstants.ParameterMode,
                new[] { EdmConstants.In, EdmConstants.Out, EdmConstants.InOut });

            InitializeEnumType(
                BuiltInTypeKind.CollectionKind,
                EdmConstants.CollectionKind,
                new[] { EdmConstants.NoneCollectionKind, EdmConstants.ListCollectionKind, EdmConstants.BagCollectionKind });

            InitializeEnumType(
                BuiltInTypeKind.PrimitiveTypeKind,
                EdmConstants.PrimitiveTypeKind,
                Enum.GetNames(typeof(PrimitiveTypeKind)));

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Bootstrapping the general facet descriptions
            ////////////////////////////////////////////////////////////////////////////////////////////////

            // Other type non-specific facets
            var generalFacetDescriptions = new FacetDescription[2];

            _nullableFacetDescription = new FacetDescription(
                DbProviderManifest.NullableFacetName,
                EdmProviderManifest.GetPrimitiveType(PrimitiveTypeKind.Boolean),
                null,
                null,
                true);
            generalFacetDescriptions[0]   = (_nullableFacetDescription);
            _defaultValueFacetDescription = new FacetDescription(
                DbProviderManifest.DefaultValueFacetName,
                GetBuiltInType(BuiltInTypeKind.EdmType),
                null,
                null,
                null);
            generalFacetDescriptions[1] = (_defaultValueFacetDescription);
            _generalFacetDescriptions   = new ReadOnlyCollection <FacetDescription>(generalFacetDescriptions);

            _collectionKindFacetDescription = new FacetDescription(
                EdmConstants.CollectionKind,
                GetBuiltInType(BuiltInTypeKind.EnumType),
                null,
                null,
                null);

            ////////////////////////////////////////////////////////////////////////////////////////////////
            // Add properties for the built-in complex types
            ////////////////////////////////////////////////////////////////////////////////////////////////
            var stringTypeUsage    = TypeUsage.Create(EdmProviderManifest.GetPrimitiveType(PrimitiveTypeKind.String));
            var booleanTypeUsage   = TypeUsage.Create(EdmProviderManifest.GetPrimitiveType(PrimitiveTypeKind.Boolean));
            var edmTypeUsage       = TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EdmType));
            var typeUsageTypeUsage = TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.TypeUsage));
            var complexTypeUsage   = TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.ComplexType));

            // populate the attributes for item attributes
            AddBuiltInTypeProperties(
                BuiltInTypeKind.MetadataProperty,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.TypeUsage, typeUsageTypeUsage),
                new EdmProperty(EdmConstants.Value, complexTypeUsage)
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.MetadataItem,
                new[]
            {
                new EdmProperty(
                    EdmConstants.ItemAttributes,
                    TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.MetadataProperty).GetCollectionType())),
                new EdmProperty(EdmConstants.Documentation, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.Documentation)))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.TypeUsage,
                new[]
            {
                new EdmProperty(EdmConstants.EdmType, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EdmType))),
                new EdmProperty(EdmConstants.Facets, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.Facet)))
            });

            //populate the attributes for the edm type
            AddBuiltInTypeProperties(
                BuiltInTypeKind.EdmType,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.Namespace, stringTypeUsage),
                new EdmProperty(EdmConstants.Abstract, booleanTypeUsage),
                new EdmProperty(EdmConstants.Sealed, booleanTypeUsage),
                new EdmProperty(EdmConstants.BaseType, complexTypeUsage)
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EnumType,
                new[] { new EdmProperty(EdmConstants.EnumMembers, stringTypeUsage) });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.CollectionType,
                new[] { new EdmProperty(EdmConstants.TypeUsage, typeUsageTypeUsage) });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.RefType,
                new[] { new EdmProperty(EdmConstants.EntityType, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EntityType))) });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EdmMember,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.TypeUsage, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.TypeUsage)))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EdmProperty,
                new[]
            {
                new EdmProperty(EdmConstants.Nullable, stringTypeUsage),
                new EdmProperty(EdmConstants.DefaultValue, complexTypeUsage)
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.NavigationProperty,
                new[]
            {
                new EdmProperty(EdmConstants.RelationshipTypeName, stringTypeUsage),
                new EdmProperty(EdmConstants.ToEndMemberName, stringTypeUsage)
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.RelationshipEndMember,
                new[]
            {
                new EdmProperty(EdmConstants.OperationBehaviors, complexTypeUsage),
                new EdmProperty(EdmConstants.RelationshipMultiplicity, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EnumType)))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EnumMember,
                new[] { new EdmProperty(EdmConstants.Name, stringTypeUsage) });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.ReferentialConstraint,
                new[]
            {
                new EdmProperty(EdmConstants.ToRole, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.RelationshipEndMember))),
                new EdmProperty(EdmConstants.FromRole, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.RelationshipEndMember))),
                new EdmProperty(
                    EdmConstants.ToProperties, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EdmProperty).GetCollectionType())),
                new EdmProperty(
                    EdmConstants.FromProperties, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EdmProperty).GetCollectionType()))
            });

            // Structural Type hierarchy
            AddBuiltInTypeProperties(
                BuiltInTypeKind.StructuralType,
                new[] { new EdmProperty(EdmConstants.Members, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EdmMember))) });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EntityTypeBase,
                new[] { new EdmProperty(EdmConstants.KeyMembers, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EdmMember))) });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.Facet,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.EdmType, edmTypeUsage),
                new EdmProperty(EdmConstants.Value, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EdmType)))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EntityContainer,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.EntitySets, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EntitySet)))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EntitySetBase,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.EntityType, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EntityType))),
                new EdmProperty(EdmConstants.Schema, stringTypeUsage),
                new EdmProperty(EdmConstants.Table, stringTypeUsage)
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.AssociationSet,
                new[]
            {
                new EdmProperty(
                    EdmConstants.AssociationSetEnds,
                    TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.AssociationSetEnd).GetCollectionType()))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.AssociationSetEnd,
                new[]
            {
                new EdmProperty(EdmConstants.Role, stringTypeUsage),
                new EdmProperty(EdmConstants.EntitySetType, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EntitySet)))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.FunctionParameter,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.Mode, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.EnumType))),
                new EdmProperty(EdmConstants.TypeUsage, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.TypeUsage)))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.EdmFunction,
                new[]
            {
                new EdmProperty(EdmConstants.Name, stringTypeUsage),
                new EdmProperty(EdmConstants.Namespace, stringTypeUsage),
                new EdmProperty(EdmConstants.ReturnParameter, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.FunctionParameter))),
                new EdmProperty(
                    EdmConstants.Parameters, TypeUsage.Create(GetBuiltInType(BuiltInTypeKind.FunctionParameter).GetCollectionType()))
            });

            AddBuiltInTypeProperties(
                BuiltInTypeKind.Documentation,
                new[]
            {
                new EdmProperty(EdmConstants.Summary, stringTypeUsage),
                new EdmProperty(EdmConstants.LongDescription, stringTypeUsage)
            });

            // Set all types to be readonly, used SetReadOnly to skip validation method to
            for (var i = 0; i < _builtInTypes.Length; i++)
            {
                _builtInTypes[i].SetReadOnly();
            }
        }
        private static TypeUsage GetFunctionTypeUsage(
            bool isModelFunction,
            System.Data.Entity.Core.SchemaObjectModel.Function somFunction,
            FacetEnabledSchemaElement somParameter,
            DbProviderManifest providerManifest,
            bool areConvertingForProviderManifest,
            System.Data.Entity.Core.SchemaObjectModel.SchemaType type,
            CollectionKind collectionKind,
            bool isRefType,
            Converter.ConversionCache convertedItemCache,
            Dictionary <SchemaElement, GlobalItem> newGlobalItems)
        {
            if (somParameter != null && areConvertingForProviderManifest && somParameter.HasUserDefinedFacets)
            {
                return(somParameter.TypeUsage);
            }
            if (type == null)
            {
                if (isModelFunction && somParameter != null && somParameter is Parameter)
                {
                    ((Parameter)somParameter).ResolveNestedTypeNames(convertedItemCache, newGlobalItems);
                    return(somParameter.TypeUsage);
                }
                if (somParameter == null || !(somParameter is ReturnType))
                {
                    return((TypeUsage)null);
                }
                ((ReturnType)somParameter).ResolveNestedTypeNames(convertedItemCache, newGlobalItems);
                return(somParameter.TypeUsage);
            }
            EdmType edmType;

            if (!areConvertingForProviderManifest)
            {
                ScalarType scalarType = type as ScalarType;
                if (scalarType != null)
                {
                    if (isModelFunction && somParameter != null)
                    {
                        if (somParameter.TypeUsage == null)
                        {
                            somParameter.ValidateAndSetTypeUsage(scalarType);
                        }
                        return(somParameter.TypeUsage);
                    }
                    if (isModelFunction)
                    {
                        ModelFunction modelFunction = somFunction as ModelFunction;
                        if (modelFunction.TypeUsage == null)
                        {
                            modelFunction.ValidateAndSetTypeUsage(scalarType);
                        }
                        return(modelFunction.TypeUsage);
                    }
                    if (somParameter != null && somParameter.HasUserDefinedFacets && somFunction.Schema.DataModel == SchemaDataModelOption.ProviderDataModel)
                    {
                        somParameter.ValidateAndSetTypeUsage(scalarType);
                        return(somParameter.TypeUsage);
                    }
                    edmType = (EdmType)Converter.GetPrimitiveType(scalarType, providerManifest);
                }
                else
                {
                    edmType = (EdmType)Converter.LoadSchemaElement(type, providerManifest, convertedItemCache, newGlobalItems);
                    if (isModelFunction && type is SchemaEnumType)
                    {
                        if (somParameter != null)
                        {
                            somParameter.ValidateAndSetTypeUsage(edmType);
                            return(somParameter.TypeUsage);
                        }
                        if (somFunction != null)
                        {
                            ModelFunction modelFunction = (ModelFunction)somFunction;
                            modelFunction.ValidateAndSetTypeUsage(edmType);
                            return(modelFunction.TypeUsage);
                        }
                    }
                }
            }
            else
            {
                edmType = !(type is TypeElement) ? (EdmType)(type as ScalarType).Type : (EdmType)(type as TypeElement).PrimitiveType;
            }
            TypeUsage typeUsage;

            if (collectionKind != CollectionKind.None)
            {
                typeUsage = convertedItemCache.GetCollectionTypeUsageWithNullFacets(edmType);
            }
            else
            {
                EntityType entityType = edmType as EntityType;
                typeUsage = entityType == null || !isRefType?convertedItemCache.GetTypeUsageWithNullFacets(edmType) : TypeUsage.Create((EdmType) new RefType(entityType));
            }
            return(typeUsage);
        }
        private static EdmProperty CreateProperty(string name, EdmType edmType)
        {
            TypeUsage typeUsage = TypeUsage.Create(edmType, new FacetValues());

            return(new EdmProperty(name, typeUsage));
        }
 internal EdmProperty(string name)
     : this(name, TypeUsage.Create((EdmType)PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)))
 {
 }
        internal void ResolveNavigationProperty(StructuralType declaringType, PropertyInfo propertyInfo)
        {
            // EdmScalarPropertyAttribute, EdmComplexPropertyAttribute and EdmRelationshipNavigationPropertyAttribute
            // are all EdmPropertyAttributes that we need to process. If the current property is not an EdmPropertyAttribute
            // we will just ignore it and skip to the next property.
            var relationshipPropertyAttributes = propertyInfo.GetCustomAttributes <EdmRelationshipNavigationPropertyAttribute>(inherit: false);

            Debug.Assert(relationshipPropertyAttributes.Count() == 1, "There should be exactly one property for every navigation property");

            // The only valid return types from navigation properties are:
            //     (1) EntityType
            //     (2) CollectionType containing valid EntityType

            // If TryGetLoadedType returned false, it could mean that we couldn't validate any part of the type, or it could mean that it's a generic
            // where the main generic type was validated, but the generic type parameter was not. We can't tell the difference, so just fail
            // with the same error message in both cases. The user will have to figure out which part of the type is wrong.
            // We can't just rely on checking for a generic because it can lead to a scenario where we report that the type parameter is invalid
            // when really it's the main generic type. That is more confusing than reporting the full name and letting the user determine the problem.
            EdmType propertyType;

            if (!TryGetLoadedType(propertyInfo.PropertyType, out propertyType)
                ||
                !(propertyType.BuiltInTypeKind == BuiltInTypeKind.EntityType ||
                  propertyType.BuiltInTypeKind == BuiltInTypeKind.CollectionType))
            {
                // Once an error is detected the property does not need to be validated further, just add to the errors
                // collection and continue with the next property. The failure will cause an exception to be thrown later during validation of all of the types.
                SessionData.EdmItemErrors.Add(
                    new EdmItemError(
                        Strings.Validator_OSpace_InvalidNavPropReturnType(
                            propertyInfo.Name, propertyInfo.DeclaringType.FullName, propertyInfo.PropertyType.FullName)));
                return;
            }
            // else we have a valid EntityType or CollectionType that contains EntityType. ResolveNonSchemaType enforces that a collection type
            // must contain an EntityType, and if it doesn't, propertyType will be null here. If propertyType is EntityType or CollectionType we know it is valid

            // Expecting EdmRelationshipNavigationPropertyAttribute to have AllowMultiple=False, so only look at first element in the attribute array

            var attribute = (EdmRelationshipNavigationPropertyAttribute)relationshipPropertyAttributes.First();

            EdmMember member = null;
            EdmType   type;

            if (SessionData.TypesInLoading.TryGetValue(attribute.RelationshipNamespaceName + "." + attribute.RelationshipName, out type)
                &&
                Helper.IsAssociationType(type))
            {
                var relationshipType = (AssociationType)type;
                if (relationshipType != null)
                {
                    // The return value of this property has been verified, so create the property now
                    var navigationProperty = new NavigationProperty(propertyInfo.Name, TypeUsage.Create(propertyType));
                    navigationProperty.RelationshipType = relationshipType;
                    member = navigationProperty;

                    if (relationshipType.Members[0].Name
                        == attribute.TargetRoleName)
                    {
                        navigationProperty.ToEndMember   = (RelationshipEndMember)relationshipType.Members[0];
                        navigationProperty.FromEndMember = (RelationshipEndMember)relationshipType.Members[1];
                    }
                    else if (relationshipType.Members[1].Name
                             == attribute.TargetRoleName)
                    {
                        navigationProperty.ToEndMember   = (RelationshipEndMember)relationshipType.Members[1];
                        navigationProperty.FromEndMember = (RelationshipEndMember)relationshipType.Members[0];
                    }
                    else
                    {
                        SessionData.EdmItemErrors.Add(
                            new EdmItemError(
                                Strings.TargetRoleNameInNavigationPropertyNotValid(
                                    propertyInfo.Name, propertyInfo.DeclaringType.FullName, attribute.TargetRoleName,
                                    attribute.RelationshipName)));
                        member = null;
                    }

                    if (member != null
                        &&
                        ((RefType)navigationProperty.FromEndMember.TypeUsage.EdmType).ElementType.ClrType != declaringType.ClrType)
                    {
                        SessionData.EdmItemErrors.Add(
                            new EdmItemError(
                                Strings.NavigationPropertyRelationshipEndTypeMismatch(
                                    declaringType.FullName,
                                    navigationProperty.Name,
                                    relationshipType.FullName,
                                    navigationProperty.FromEndMember.Name,
                                    ((RefType)navigationProperty.FromEndMember.TypeUsage.EdmType).ElementType.ClrType)));
                        member = null;
                    }
                }
            }
            else
            {
                SessionData.EdmItemErrors.Add(
                    new EdmItemError(
                        Strings.RelationshipNameInNavigationPropertyNotValid(
                            propertyInfo.Name, propertyInfo.DeclaringType.FullName, attribute.RelationshipName)));
            }

            if (member != null)
            {
                declaringType.AddMember(member);
            }
        }
 private FunctionParameter CreateAggregateParameter(PrimitiveTypeKind collectionParameterTypeElementTypeKind)
 {
     return(new FunctionParameter(
                "collection", TypeUsage.Create(primitiveTypes[(int)collectionParameterTypeElementTypeKind].EdmType.GetCollectionType()),
                ParameterMode.In));
 }
Esempio n. 23
0
        private void CreateAndAddNavigationProperty(
            StructuralType cspaceType, StructuralType ospaceType, NavigationProperty cspaceProperty)
        {
            EdmType ospaceRelationship;

            if (CspaceToOspace.TryGetValue(cspaceProperty.RelationshipType, out ospaceRelationship))
            {
                Debug.Assert(ospaceRelationship is StructuralType, "Structural type expected.");

                var     foundTarget = false;
                EdmType targetType  = null;
                if (Helper.IsCollectionType(cspaceProperty.TypeUsage.EdmType))
                {
                    EdmType findType;
                    foundTarget =
                        CspaceToOspace.TryGetValue(
                            ((CollectionType)cspaceProperty.TypeUsage.EdmType).TypeUsage.EdmType, out findType);
                    if (foundTarget)
                    {
                        Debug.Assert(findType is StructuralType, "Structural type expected.");

                        targetType = findType.GetCollectionType();
                    }
                }
                else
                {
                    EdmType findType;
                    foundTarget = CspaceToOspace.TryGetValue(cspaceProperty.TypeUsage.EdmType, out findType);
                    if (foundTarget)
                    {
                        Debug.Assert(findType is StructuralType, "Structural type expected.");

                        targetType = findType;
                    }
                }

                Debug.Assert(
                    foundTarget,
                    "Since the relationship will only be created if it can find the types for both ends, we will never fail to find one of the ends");

                var navigationProperty = new NavigationProperty(cspaceProperty.Name, TypeUsage.Create(targetType));
                var relationshipType   = (RelationshipType)ospaceRelationship;
                navigationProperty.RelationshipType = relationshipType;

                // we can use First because o-space relationships are created directly from
                // c-space relationship
                navigationProperty.ToEndMember =
                    (RelationshipEndMember)relationshipType.Members.First(e => e.Name == cspaceProperty.ToEndMember.Name);
                navigationProperty.FromEndMember =
                    (RelationshipEndMember)relationshipType.Members.First(e => e.Name == cspaceProperty.FromEndMember.Name);
                ospaceType.AddMember(navigationProperty);
            }
            else
            {
                var missingType =
                    cspaceProperty.RelationshipType.RelationshipEndMembers.Select(e => ((RefType)e.TypeUsage.EdmType).ElementType).First(
                        e => e != cspaceType);
                LogError(
                    Strings.Validator_OSpace_Convention_RelationshipNotLoaded(
                        cspaceProperty.RelationshipType.FullName, missingType.FullName),
                    missingType);
            }
        }
Esempio n. 24
0
 internal EdmProperty(string name)
     : this(name, TypeUsage.Create(PrimitiveType.GetEdmPrimitiveType(PrimitiveTypeKind.String)))
 {
     // testing only
 }
Esempio n. 25
0
 internal CollectionType(EdmType elementType)
     : this(TypeUsage.Create(elementType))
 {
     DataSpace = elementType.DataSpace;
 }
Esempio n. 26
0
        internal void ResolveNavigationProperty(StructuralType declaringType, PropertyInfo propertyInfo)
        {
            IEnumerable <EdmRelationshipNavigationPropertyAttribute> customAttributes = propertyInfo.GetCustomAttributes <EdmRelationshipNavigationPropertyAttribute>(false);
            EdmType edmType;

            if (!this.TryGetLoadedType(propertyInfo.PropertyType, out edmType) || edmType.BuiltInTypeKind != BuiltInTypeKind.EntityType && edmType.BuiltInTypeKind != BuiltInTypeKind.CollectionType)
            {
                this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.Validator_OSpace_InvalidNavPropReturnType((object)propertyInfo.Name, (object)propertyInfo.DeclaringType.FullName, (object)propertyInfo.PropertyType.FullName)));
            }
            else
            {
                EdmRelationshipNavigationPropertyAttribute propertyAttribute = customAttributes.First <EdmRelationshipNavigationPropertyAttribute>();
                EdmMember member = (EdmMember)null;
                EdmType   type;
                if (this.SessionData.TypesInLoading.TryGetValue(propertyAttribute.RelationshipNamespaceName + "." + propertyAttribute.RelationshipName, out type) && Helper.IsAssociationType(type))
                {
                    AssociationType associationType = (AssociationType)type;
                    if (associationType != null)
                    {
                        NavigationProperty navigationProperty = new NavigationProperty(propertyInfo.Name, TypeUsage.Create(edmType));
                        navigationProperty.RelationshipType = (RelationshipType)associationType;
                        member = (EdmMember)navigationProperty;
                        if (associationType.Members[0].Name == propertyAttribute.TargetRoleName)
                        {
                            navigationProperty.ToEndMember   = (RelationshipEndMember)associationType.Members[0];
                            navigationProperty.FromEndMember = (RelationshipEndMember)associationType.Members[1];
                        }
                        else if (associationType.Members[1].Name == propertyAttribute.TargetRoleName)
                        {
                            navigationProperty.ToEndMember   = (RelationshipEndMember)associationType.Members[1];
                            navigationProperty.FromEndMember = (RelationshipEndMember)associationType.Members[0];
                        }
                        else
                        {
                            this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.TargetRoleNameInNavigationPropertyNotValid((object)propertyInfo.Name, (object)propertyInfo.DeclaringType.FullName, (object)propertyAttribute.TargetRoleName, (object)propertyAttribute.RelationshipName)));
                            member = (EdmMember)null;
                        }
                        if (member != null && ((RefType)navigationProperty.FromEndMember.TypeUsage.EdmType).ElementType.ClrType != declaringType.ClrType)
                        {
                            this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.NavigationPropertyRelationshipEndTypeMismatch((object)declaringType.FullName, (object)navigationProperty.Name, (object)associationType.FullName, (object)navigationProperty.FromEndMember.Name, (object)((RefType)navigationProperty.FromEndMember.TypeUsage.EdmType).ElementType.ClrType)));
                            member = (EdmMember)null;
                        }
                    }
                }
                else
                {
                    this.SessionData.EdmItemErrors.Add(new EdmItemError(Strings.RelationshipNameInNavigationPropertyNotValid((object)propertyInfo.Name, (object)propertyInfo.DeclaringType.FullName, (object)propertyAttribute.RelationshipName)));
                }
                if (member == null)
                {
                    return;
                }
                declaringType.AddMember(member);
            }
        }