如何在OData服务中将多个名称空间添加到IEdmModel中

时间:2015-02-17 12:25:06

标签: c# odata asp.net-web-api

目前我正在使用带有WebApi的OData,我一直坚持在WebApiConfig文件中将多个名称空间添加到IEdmModel GetModel()。以下是我目前正在使用的方法

public static IEdmModel GetModel()
        {
            ODataModelBuilder builder = new ODataConventionModelBuilder();

           var product= builder.EntitySet<Product>("Products");
            //product.EntityType.HasKey(pkg => pkg.ID);
            //product.EntityType.HasKey(pkg => pkg.Code);

            builder.EntitySet<Customer>("Customers");
            builder.EntitySet<CustomerAddress>("CustomerAddresses");
            builder.EntitySet<CustomerEmail>("CustomerEmails");
            builder.EntitySet<CustomerPhone>("CustomerPhones");
            builder.EntitySet<Country>("Countries");
            builder.EntitySet<State>("States");
            builder.EntitySet<CustomerStore>("CustomerStores");

            builder.Namespace = "StateService";
            builder.EntityType<State>().Collection.Function("GetStatesByCountry").ReturnsCollectionFromEntitySet<State>("States");

            builder.Namespace = "ProductCategoryService";
            builder.EntityType<ProductCategory>().Collection.Function("GetProductCategories").ReturnsCollectionFromEntitySet<ProductCategory>("ProductCategories");


            return builder.GetEdmModel();
        }

如果我添加另一个名称空间,首先会覆盖

 builder.Namespace = "StateService";
builder.Namespace = "ProductCategoryService";

我的问题是有没有办法避免这个问题。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:3)

WebAPI OData可以为每个entityType,complexType设置名称空间,例如:

// Set the default namespace 
builder.Namespace = "Default";
builder.EntitySet<Product>("Products");

var prodType = builder.EntityType<Product>();
// Set the namespace for type product
prodType.Namespace = "ProductNamespace";