0

具有多对多关系的域对象:

public class Customer 
{
    public int Id { get; set; }
    public string CompanyName { get; set; }
    public string Phone { get; set; }
    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Customer> Customers { get; set; }
}

控制器:

public class CustomersController : EntitySetController<Customer, int>
{
    // .. omited

    [HttpGet]
    public IQueryable<Customer> GetByTag([FromODataUri] string tagName)
    {
        tagName = tagName.Replace("#", "");
        return _Context.Customers.Where(p => p.Tags.Any(t => t.Name.Contains(tagName)));
    }
}

这是因为我使用 Breeze 库进行 odata 请求,而她不支持odata 方法any

我的配置:

public static class BreezeWebApiConfig
{
    public static void RegisterBreezePreStart()
    {
        GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "BreezeApi",
            routeTemplate: "api/{controller}/{action}"
        );
    }
}

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapODataRoute("odata", "odata", GetEdmModel());
        config.EnableQuerySupport();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

    public static IEdmModel GetEdmModel()
    {
        ODataModelBuilder builder = new ODataConventionModelBuilder();

        builder.EntitySet<Customer>("Customers");
        var customersByTagAction = builder.Entity<Customer>().Collection.Action("GetByTag");
        customersByTagAction.Parameter<string>("tagName");
        customersByTagAction.ReturnsCollectionFromEntitySet<Customer>("Customers");

        builder.EntitySet<Tag>("Tags");
        builder.Namespace = "WebAPIODataWithBreezeConsumer.Models";
        return builder.GetEdmModel();
    }
}

要求

/odata/Customers/GetByTag?$orderby=CompanyName&$expand=Tags&$select=Id,CompanyName,Phone,Tags/Id,Tags/Name&tagName=#5

问题

我究竟做错了什么?为什么会出现错误 501?

在我的课堂 WebApiConfig 中。我需要这个代码EnableQuerySupport吗?为什么我需要启用它?

4

1 回答 1

0

Breeze does not yet support many-many relationships. As a workaround you can use two 1-many relationships. There is a User Voice issue on this topic; here Please vote for it.

于 2013-07-10T21:26:10.100 回答