/// <summary>
 /// Adds Associations to the EDM model based on an Entity Data Model Schema
 /// </summary>
 /// <param name="model">EDM model to append to</param>
 /// <param name="model">Entity Data Model Schema to get associations from</param>
 private static void AddNavigationProperties(ConstructableMetadata model, IEdmEntityType entityType, IEnumerable<NavigationProperty> properties)
 {
     foreach (NavigationProperty property in properties)
     {
         if (property.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
         {
             model.AddResourceSetReferenceProperty(
                 entityType,
                 property.Name,
                 model.FindEntitySet(property.ToEndMember.GetEntityType().Name),
                 model.FindType(property.ToEndMember.GetEntityType().FullName) as IEdmEntityType
             );
         }
         else
         {
             model.AddResourceReferenceProperty(
                 entityType,
                 property.Name,
                 model.FindEntitySet(property.ToEndMember.GetEntityType().Name),
                 model.FindType(property.ToEndMember.GetEntityType().FullName) as IEdmEntityType
             );
         }
     }
 }
        /// <summary>
        /// Creates an <see cref="IEdmModel"/> based on a combination of the ObjectContext's properties and it's metadata workspace
        /// </summary>
        /// <param name="context">The object context instance</param>
        /// <returns>An <see cref="IEdmModel"/> based on a combination of the ObjectContext's properties and it's metadata workspace</returns>
        public static IEdmModel CreateModel(ObjectContext context)
        {
            Assembly objectLayerAssembly = context.GetType().Assembly;
            ConstructableMetadata metadata = new ConstructableMetadata(context.DefaultContainerName, ("DBNorthwindModel"));
            foreach (EntityType entityType in context.MetadataWorkspace.GetItems<EntityType>(DataSpace.CSpace))
            {
                Type clrType = objectLayerAssembly.GetType("Microsoft.Test.Taupo.OData.WCFService.Model." + entityType.Name, false, true);
                IEdmEntityType edmEntityType = metadata.AddEntityType(entityType.Name, clrType, null, false, "DBNorthwindModel");
                AddProperties(metadata, entityType.Properties, edmEntityType);
            }

            foreach (var entitySet in context.GetType().GetProperties().Where(property => property.PropertyType.GetInterface("IQueryable") != null))
            {
                Type entityType = entitySet.PropertyType.GetGenericArguments().First();
                IEdmEntityType edmEntityType = metadata.FindType("DBNorthwindModel." + entityType.Name) as IEdmEntityType;
                metadata.AddEntitySet(entitySet.Name, edmEntityType);
            }

            return metadata;
        }
 /// <summary>
 /// Adds a <paramref name="property"/> to <paramref name="metadata"/>
 /// under type <paramref name="structuredType"/>
 /// </summary>
 /// <param name="metadata">Metadata document to be added to</param>
 /// <param name="property">Property to add</param>
 /// <param name="structuredType">Structured type to add property to</param>
 private static void AddProperty(ConstructableMetadata metadata, EdmProperty property, IEdmStructuredType structuredType)
 {
     if (property.BuiltInTypeKind == BuiltInTypeKind.EdmProperty)
     {
         AddPrimitiveProperty(metadata, DataTypeToSystemType((PrimitiveType)property.TypeUsage.EdmType, IsNullable(property.TypeUsage)), property, structuredType);
     }
     else if (property.BuiltInTypeKind == BuiltInTypeKind.ComplexType)
     {
         metadata.AddComplexProperty(structuredType, property.Name, metadata.FindType(property.TypeUsage.EdmType.FullName) as IEdmComplexType);
     }
 }
Beispiel #4
0
        private static void ProcessComplexProperty(ConstructableMetadata model, EdmStructuredType owningType, ODataComplexValue complexValue, bool isOpenProperty)
        {
            string namespaceName = "";
            var schematype = owningType as IEdmSchemaType;
            if (schematype != null)
            {
                namespaceName = schematype.Namespace;
            }

            ExceptionUtilities.Assert(complexValue.TypeName.StartsWith(namespaceName + "."), "The type name must start with the same namespace as its owning type.");

            EdmComplexType complexType = model.FindType(complexValue.TypeName) as EdmComplexType;
            if (complexType == null)
            {
                string complexTypeLocalName = complexValue.GetUnqualifiedTypeName(namespaceName);
                complexType = model.AddComplexType(complexTypeLocalName, null, null, false) as EdmComplexType;
                CreateMetadataProperties(model, complexType, complexValue.Properties);
            }

            if (isOpenProperty)
            {
                //TODO: Find way to set parent type to open.
            }
            else
            {
                ExceptionUtilities.CheckObjectNotNull(complexType, "Complex type cannot be null");
                owningType.AddProperty(new EdmStructuralProperty(owningType, complexType.Name, complexType.ToTypeReference()));
            }
        }