DataServiceProvider.DSPMetadata.AddPrimitiveProperty C# (CSharp) Method

AddPrimitiveProperty() public method

Adds a primitive property to the specified resourceType.
public AddPrimitiveProperty ( System.Data.Services.Providers.ResourceType resourceType, string name, Type propertyType ) : void
resourceType System.Data.Services.Providers.ResourceType The resource type to add the property to.
name string The name of the property to add.
propertyType System.Type The CLR type of the property to add. This can be only a primitive type.
return void
        public void AddPrimitiveProperty(ResourceType resourceType, string name, Type propertyType)
        {
            this.AddPrimitiveProperty(resourceType, name, propertyType, false);
        }

Same methods

DSPMetadata::AddPrimitiveProperty ( System.Data.Services.Providers.ResourceType resourceType, string name, Type propertyType, bool isKey ) : void

Usage Example

        protected override void PopulateMetadata(DSPMetadata metadata, MongoContext context)
        {
            foreach (var collectionName in GetCollectionNames(context))
            {
                var collection = context.Database.GetCollection(collectionName);
                var document = collection.FindOne();
                if (document != null)
                {
                    var collectionType = metadata.AddEntityType(collectionName);

                    foreach (var element in document.Elements)
                    {
                        var elementType = GetElementType(context, element);
                        if (element.Name == "_id")
                        {
                            metadata.AddKeyProperty(collectionType, "ID", elementType);
                        }
                        else if (elementType == typeof(BsonDocument))
                        {
                            string referencedCollectionName = GetDocumentCollection(context, element.Value.AsBsonDocument).Name;
                            resourceReferences.Add(new Tuple<ResourceType, string, string>(collectionType, element.Name, referencedCollectionName));
                        }
                        else if (elementType == typeof(BsonArray))
                        {
                            var bsonArray = element.Value.AsBsonArray;
                            if (bsonArray != null && bsonArray.Count > 0)
                            {
                                string referencedCollectionName = GetDocumentCollection(context, bsonArray[0].AsBsonDocument).Name;
                                resourceSetReferences.Add(new Tuple<ResourceType, string, string>(collectionType, element.Name, referencedCollectionName));
                            }
                        }
                        else
                        {
                            metadata.AddPrimitiveProperty(collectionType, element.Name, elementType);
                        }
                    }
                    metadata.AddResourceSet(collectionName, collectionType);
                }
            }

            foreach (var reference in resourceReferences)
            {
                var referencedResourceSet = metadata.ResourceSets.Where(x => x.Name == reference.Item3).SingleOrDefault();
                if (referencedResourceSet != null)
                {
                    metadata.AddResourceSetReferenceProperty(reference.Item1, reference.Item2, referencedResourceSet);
                }
            }

            foreach (var reference in resourceSetReferences)
            {
                var referencedResourceSet = metadata.ResourceSets.Where(x => x.Name == reference.Item3).SingleOrDefault();
                if (referencedResourceSet != null)
                {
                    metadata.AddResourceSetReferenceProperty(reference.Item1, reference.Item2, referencedResourceSet);
                }
            }
        }