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

目前我正在使用带有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";

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

解决方法:

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";
上一篇:c# – 错误:无法将PATCH应用于导航属性.那我该怎么办?


下一篇:c# – LINQ to Azure Search