How to add multiple namespaces in IEdmModel in OData service

I am currently working with OData using WebApi, and I am stuck with adding multiple namespaces in the IEdmModel GetModel()WebApiConfig file. Below is the method that I am currently using

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();
        }

If I add another namespace, it will override

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

My problem is whether there is any approach to avoid this problem. Any help would be greatly appreciated.

+4
source share
1 answer

WebAPI OData can set a namespace for each type of entityType, complexType, for example:

// 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";
+2
source

All Articles