AddResourceSetReferenceProperty() public method

Adds a resource set reference property to the specified resourceType.
This creates a property pointing to multiple resources in the target resource set.
public AddResourceSetReferenceProperty ( System.Data.Services.Providers.ResourceType resourceType, string name, System.Data.Services.Providers.ResourceSet targetResourceSet ) : void
resourceType System.Data.Services.Providers.ResourceType The resource type to add the property to.
name string The name of the property to add.
targetResourceSet System.Data.Services.Providers.ResourceSet The resource set the resource reference property points to.
return void
        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);
                }
            }
        }