コード例 #1
0
        private static XElement AddProperty(string propertyName, IType propertyType, object value)
        {
            var propertyElement = new XElement(
                ODataService.NsDataServices + propertyName
                );

            if (propertyType.ReturnedClass != typeof(string))
            {
                propertyElement.Add(new XAttribute(ODataService.NsMetadata + "type", LiteralUtil.GetEdmType(propertyType.ReturnedClass)));
            }

            string serialized = LiteralUtil.SerializeValue(value);

            if (serialized == null)
            {
                propertyElement.Add(new XAttribute(ODataService.NsMetadata + "null", "true"));
            }
            else
            {
                propertyElement.Add(new XText(serialized));
            }
            return(propertyElement);
        }
コード例 #2
0
        private string CreateMetadataResponse()
        {
            var schemaElement = new XElement(
                NsEdm + "Schema",
                new XAttribute(XNamespace.Xmlns + "d", NsDataServices),
                new XAttribute(XNamespace.Xmlns + "m", NsMetadata),
                new XAttribute("xmlns", NsEdm),
                new XAttribute("Namespace", SchemaNamespace)
                );

            var entityContainerElement = new XElement(
                NsEdm + "EntityContainer",
                new XAttribute("Name", ContainerName),
                new XAttribute(NsMetadata + "IsDefaultEntityContainer", "true")
                );

            var associations = new Dictionary <string, XElement>();

            foreach (var type in _sessionFactory.GetAllClassMetadata().Values)
            {
                var persister = GetPersister(type);

                string entityName = persister.EntityType.ReturnedClass.Name;

                var entityElement = new XElement(
                    NsEdm + "EntityType",
                    new XAttribute("Name", entityName),
                    new XElement(
                        NsEdm + "Key",
                        new XElement(
                            NsEdm + "PropertyRef",
                            new XAttribute("Name", persister.IdentifierPropertyName)
                            )
                        )
                    );

                schemaElement.Add(entityElement);

                entityElement.Add(new XElement(
                                      NsEdm + "Property",
                                      new XAttribute("Name", persister.IdentifierPropertyName),
                                      new XAttribute("Type", LiteralUtil.GetEdmType(persister.IdentifierType.ReturnedClass)),
                                      new XAttribute("Nullable", "false")
                                      ));

                foreach (var property in persister.EntityMetamodel.Properties)
                {
                    var collectionType = property.Type as CollectionType;
                    var manyToOneType  = property.Type as ManyToOneType;

                    var propertyType = property.Type.ReturnedClass;

                    if (collectionType != null || manyToOneType != null)
                    {
                        string fromRole;
                        string toRole;
                        string relationship;
                        string multiplicity;

                        if (collectionType != null)
                        {
                            propertyType = propertyType.GetGenericArguments()[0];

                            fromRole     = entityName + "_" + property.Name;
                            toRole       = propertyType.Name + "_" + entityName;
                            relationship = toRole + "_" + fromRole;
                            multiplicity = "*";
                        }
                        else
                        {
                            fromRole     = entityName + "_" + property.Name;
                            toRole       = propertyType.Name + "_" + Inflector.Pluralize(entityName);
                            relationship = fromRole + "_" + toRole;
                            multiplicity = "0..1";
                        }

                        entityElement.Add(new XElement(
                                              NsEdm + "NavigationProperty",
                                              new XAttribute("Name", property.Name),
                                              new XAttribute("Relationship", SchemaNamespace + "." + relationship),
                                              new XAttribute("FromRole", fromRole),
                                              new XAttribute("ToRole", toRole)
                                              ));

                        XElement association;

                        if (!associations.TryGetValue(relationship, out association))
                        {
                            association = new XElement(
                                NsEdm + "Association",
                                new XAttribute("Name", relationship)
                                );

                            associations.Add(relationship, association);
                        }

                        association.Add(new XElement(
                                            NsEdm + "End",
                                            new XAttribute("Role", toRole),
                                            new XAttribute("Type", SchemaNamespace + "." + propertyType.Name),
                                            new XAttribute("Multiplicity", multiplicity)
                                            ));
                    }
                    else
                    {
                        entityElement.Add(new XElement(
                                              NsEdm + "Property",
                                              new XAttribute("Name", property.Name),
                                              new XAttribute("Type", LiteralUtil.GetEdmType(propertyType)),
                                              new XAttribute("Nullable", property.IsNullable ? "true" : "false")
                                              ));
                    }
                }

                entityContainerElement.Add(new XElement(
                                               NsEdm + "EntitySet",
                                               new XAttribute("Name", Inflector.Pluralize(entityName)),
                                               new XAttribute("EntityType", SchemaNamespace + "." + entityName)
                                               ));
            }

            foreach (var association in associations.Values)
            {
                schemaElement.Add(association);
            }

            schemaElement.Add(entityContainerElement);

            var document = new XDocument(
                new XElement(
                    NsEdmx + "Edmx",
                    new XAttribute(XNamespace.Xmlns + "edmx", NsEdmx),
                    new XAttribute("Version", "1.0"),
                    new XElement(
                        NsEdmx + "DataServices",
                        new XAttribute(XNamespace.Xmlns + "m", NsMetadata),
                        new XAttribute(NsMetadata + "DataServiceVersion", "2.0"),
                        schemaElement
                        )
                    )
                );

            return(document.ToString(SaveOptions.DisableFormatting));
        }