使用IEdmModel将EntitySet名称解析为EntityName(产品 - >产品)

时间:2015-04-21 09:27:45

标签: c# .net odata wcf-data-services

我在Entity Framework

之上使用OData服务
var uri = new Uri("http://localhost:9876/Service.svc");
var context = new DataServiceContext(uri, , DataServiceProtocolVersion.V3);
var model = EdmxReader.Parse(
    XmlReader.Create(context.GetMetadataUri().AbsoluteUri)
);
context.Format.UseJson(model);

现在我需要从EntitySet名称

中找出实体名称

我的实体名为ProductCustomer,但EntitySet的名称可以是ProductsCustomerSet或其他名称。

由于我已经加载了IEdmModel并且信息位于$metadata,有没有办法从IEdmModel解析实体名称?

1 个答案:

答案 0 :(得分:1)

由于我还没有找到提取所需信息的方法,所以我最终创建了自己的解决方案。

首先,我将元数据xml读入一个字符串,只能进行服务往返。 然后我从字符串创建我的IEdmModel并创建我自己的类,它从元数据xml中提取所有信息。

    var client = new WebClient();
    var metadata = client.DownloadString(metadataUri);
    var model = CreateModel(metadata);
    var schema = CreateSchema(metadata);

    private static IEdmModel CreateModel(string metadata)
    {
        using (var reader = new StringReader(metadata))
        using (var xmlReader = XmlReader.Create(reader))
        {
            return EdmxReader.Parse(xmlReader);
        }
    }

    private static Schema.Schema CreateSchema(string metadata)
    {
        using (var reader = new StringReader(metadata))
        using (var xmlReader = XmlReader.Create(reader))
        {
            var root = XElement.Load(xmlReader);
            return SchemaBuilder.GetSchema(root);
        }
    }

以下是从元数据中读取Schema的代码,希望有人会觉得这很有用。我没有包含我使用的类,但它们只是没有代码的POCOS。

    public static Schema GetSchema(XElement root)
    {

        XNamespace edmx = root.GetNamespaceOfPrefix("edmx");
        XNamespace edm = root.Element(edmx + "DataServices").Elements().First().GetDefaultNamespace();

        var result = from s in root.Element(edmx + "DataServices").Elements(edm + "Schema")
                     select new
                     {
                         Namespace = (string)s.Attribute("Namespace"),
                         EntityTypes = from e in s.Elements(edm + "EntityType")
                                       select new EntityType
                                       {
                                           Name = (string)e.Attribute("Name"),
                                           Key = from k in e.Element(edm + "Key").Elements(edm + "PropertyRef")
                                                 select (string)k.Attribute("Name"),
                                           Properties = from p in e.Elements(edm + "Property")
                                                        select new Property
                                                        {
                                                            Name = (string)p.Attribute("Name"),
                                                            Type = (string)p.Attribute("Type"),
                                                            Nullable = (bool)p.Attribute("Nullable", true),
                                                            MaxLength = (string)p.Attribute("MaxLength"),
                                                            FixedLength = (bool)p.Attribute("FixedLength", false),
                                                        },
                                           NavigationProperties = from p in e.Elements(edm + "NavigationProperty")
                                                                  select new NavigationProperty
                                                                  {
                                                                      Name = (string)p.Attribute("Name"),
                                                                      Relationship = (string)p.Attribute("Relationship"),
                                                                      ToRole = (string)p.Attribute("ToRole"),
                                                                      FromRole = (string)p.Attribute("FromRole"),
                                                                  }
                                       },
                         Associations = from a in s.Elements(edm + "Association")
                                        select new Association
                                        {
                                            Name = (string)a.Attribute("Name"),
                                            Ends = from et in a.Elements(edm + "End")
                                                   select new AssociationEnd
                                                   {
                                                       Type = (string)et.Attribute("Type"),
                                                       Role = (string)et.Attribute("Role"),
                                                       Multiplicity = (string)et.Attribute("Multiplicity"),
                                                   }
                                        },
                         AssociationSets = from @as in s.Elements(edm + "EntityContainer").Elements(edm + "AssociationSet")
                                           select new AssociationSet
                                           {
                                               Name = (string)@as.Attribute("Name"),
                                               Association = (string)@as.Attribute("Association"),
                                               Ends = from r in @as.Elements(edm + "End")
                                                      select new AssociationSetEnd
                                                      {
                                                          Role = (string)r.Attribute("Role"),
                                                          EntitySet = (string)r.Attribute("EntitySet"),
                                                      },
                                           },
                        EntitySets = from @es in s.Elements(edm + "EntityContainer").Elements(edm + "EntitySet")
                                     select new EntitySet
                                            {
                                                Name = (string)@es.Attribute("Name"),
                                                EntityType = (string)@es.Attribute("EntityType"),
                                            },

                     };

        return new Schema
        {
            Namespace = result.First().Namespace,
            EntityTypes = result.SelectMany(x => x.EntityTypes).ToDictionary(x => x.Name),
            Associations = result.SelectMany(x => x.Associations).ToDictionary(x => x.Name),
            AssociationSets = result.SelectMany(x => x.AssociationSets).ToDictionary(x => x.Name),
            EntitySets = result.SelectMany(x => x.EntitySets).ToDictionary(x => x.Name),
        };
    }