سؤال

FINAL EDIT : I will put the answer here at the top for other people who are searching. The main problem was the namespace mismatch caused by db-first generated EDM attaching '.Models' to the end of the model namespace. This namespace did not matched with the odata namespace so the route was failing. I just edited out all the occurances of '.Models' from the namespace and now it's working.


A newbie trying out Breeze with webApiOdata set up. Sorry if this question is a trivial one.

I have a db generated edmx model with webapi odata controllers. I was having problem with getting the correct metadata to show until I read about the new Breeze EdmBuilder.

That solved the problem of getting the correct metadata to show but now, I can not route to any of the tables. If I try /odata/Customers I get a 406 error.

Before, I was using ODataConventionModelBuilder to set the EntitySets and that worked fine.

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<Detail>("Details");
    builder.EntitySet<Item>("Items");
    builder.EntitySet<Order>("Orders");
    builder.EntitySet<Customer>("Customers");

Now, since I am using EdmBuilder, how do I set the EntitySets so that I can route to proper data?

I hope the question makes sense.

* EDIT : I have added the listing of GCSodContext and a snippet from the Customers controller.

namespace GCSbz3.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class GCSodContext : DbContext
    {
        public GCSodContext()
            : base("name=GCSodContext")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<Detail> Details { get; set; }
        public virtual DbSet<Item> Items { get; set; }
        public virtual DbSet<Order> Orders { get; set; }
    }
}

and Here is the Customers controller

...
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using GCSbz3.Models;

namespace GCSbz3.Controllers
{
    public class CustomersController : ODataController
    {
        private GCSodContext db = new GCSodContext();

        // GET odata/Customers
        [Queryable]
        public IQueryable<Customer> GetCustomers()
        {
            return db.Customers;
        }
...

Here is the Customer class.

namespace GCSbz3.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Customer
    {
        public Customer()
        {
            this.Orders = new HashSet<Order>();
        }

        public int CustID { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public string Phone { get; set; }

        public virtual ICollection<Order> Orders { get; set; }
    }
}
هل كانت مفيدة؟

المحلول

Check your ODataController name, it needs to be aligned with EntitySet name in your edmx.

If the set name is CustomerSet, controller needs to be CustomerSetController with a public GetCustomerSet method.

For instance this works;

TestDbContext.cs

public partial class TestDbContext : DbContext
{
    public TestDbContext()
        : base("name=TestDbContext")
    {}

    public virtual DbSet<A1> A1Set { get; set; }
}

A1SetController.cs

public class A1SetController : ODataController
{
    private TestDbContext db = new TestDbContext();

    // GET odata/A1Set
    [Queryable]
    public IQueryable<A1> GetA1Set()
    {
        return db.A1Set;
    }
}

And routing setup in WebApiConfig.cs

// OData routes
config.Routes.MapODataRoute(
    routeName: "odata",
    routePrefix: "odata",
    model: EdmBuilder.GetEdm<TestDbContext>(),
    batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

And last, edmx and actual model (class) namespaces need to be aligned. When you make a request to $metadata, you see the defined namespace for your model. You can change it in your Model Designer, right click to an empty area, click to Properties. In Properties window, you can see Namespace attribute. In my case;

<Schema Namespace="Web">
    <EntityType Name="Customer">

And Customer.cs

namespace Web
{
    using System;
    using System.Collections.Generic;

    public partial class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top