コード例 #1
0
        /// <summary>
        /// Converts the <paramref name="entityModelSchema"/> to the corresponding
        /// <see cref="IEdmModel"/> including the OData metadata annotations.
        /// </summary>
        /// <param name="entityModelSchema">The <see cref="EntityModelSchema"/> to convert.</param>
        /// <param name="loadSerializableAnnotations">true if serializable annotations should be loaded into their user-friendly in-memory representation; otherwise false.</param>
        /// <returns>The <see cref="IEdmModel"/> instance that corresponds to the <paramref name="entityModelSchema"/>.</returns>
        public virtual IEdmModel Convert(EntityModelSchema entityModelSchema, bool loadSerializableAnnotations = true)
        {
            // TODO: This code is way too complex for what it does.
            //      The current implementation for the underlying converter does not directly convert from EntityModelSchema
            //      to IEdmModel; instead it serializes the entity model schema into Xml and then parses the Xml into an IEdmModel.
            //      Direct conversion of annotations does not work because of this. We need to go from test annotations on the
            //      entity model schema to attribute annotations on that entity model schema, then use the underlying converter,
            //      then convert the serializable annotations on the EDM model back into product annotations as needed.
            //
            //      Talk to the EdmLib team whether we can use a direct converter instead!
            //
            //      Also modify the implementation of DataServiceProviderFactory once this is fixed; we do another post-processing
            //      there that could be folded into a visitor pattern.

            if (entityModelSchema == null)
            {
                return(null);
            }


            // 1) Convert OData-specific test annotations to attribute annotations (this clones the model if needed)
            entityModelSchema = this.SerializableToTestAnnotationConverter.ConvertToProductAnnotations(entityModelSchema);

            // 2) Use the underlying converter to convert from entity model schema to Edm model
            this.BaseEntityModelSchemaToEdmModelConverter.EdmVersion = entityModelSchema.MinimumVersion().ToEdmVersion();
            IEdmModel model = this.BaseEntityModelSchemaToEdmModelConverter.ConvertToEdmModel(entityModelSchema);

            Debug.Assert(model != null, "Expected non-null model");

            if (loadSerializableAnnotations)
            {
                // 4) Because we have to go through Xml we cannot ensure the same order of the EPM annotations on the entity
                //    types we have to go fix this now.
                FixEPMAnnotationOrder(entityModelSchema, model);
            }

            return(model);
        }