c# - 如何将多个命名空间添加到 OData 服务中的 IEdmModel

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

目前我正在使用 OData 和 WebApi,我一直坚持将多个命名空间添加到 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# - 如何将多个命名空间添加到 OData 服务中的 IEdmModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28561437/

相关文章:

c# - 根据不存在的空间拆分乌尔都语单词

odata - 使用 OData 查询名称包含空格的字段

ssis - 使用 oData 源连接器驱动程序通过 SSIS 进行 SQL Sharepoint 集成

c# - 如何在 ASP.NET Web API 上获取对象?

.net - 为批量操作实现网络 Hook

json - 从 Web API 返回驼峰式 JSON

c# - 在 WPF 中实现 CheckBoxes 的 ListBox

c# - byte[] 到无符号 BigInteger?

c# - 存储在同一位置的 Azure 缓存中的 session 在多个实例上不同步

date - 在 UI5 中格式化 "/Date(timestamp)/"值