目前我正在使用 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/