$expand with breeze js

Topics: ASP.NET Web API
Jul 18, 2013 at 8:27 PM
hi
i have downloaded vs2013 with all the beta's
i have created an ODataController and using basic breezejs with datajs as client
i have tried to work with expand and i am not getting and results although in the network all looks ok
i am building my own model

private static Microsoft.Data.Edm.IEdmModel GetExplicitEDM()
    {


        ODataModelBuilder builder = new ODataModelBuilder();

        EntitySetConfiguration<Product> ProductconfigSet = builder.EntitySet<Product>("Product");
        ProductconfigSet.HasIdLink(eic => eic.GenerateSelfLink(false), true);
        ProductconfigSet.EntityType.HasKey(s => s.ID);
        ProductconfigSet.EntityType.Property(s => s.Name);

        EntitySetConfiguration<Store> StoreconfigSet = builder.EntitySet<Store>("Store");
        StoreconfigSet.HasIdLink(eic => eic.GenerateSelfLink(false), true);
        StoreconfigSet.EntityType.HasKey(s => s.ID);
        StoreconfigSet.EntityType.Property(s => s.Name);

        var hasMany = StoreconfigSet.EntityType.HasMany(s => s.Products);
        StoreconfigSet.HasNavigationPropertyLink(hasMany, (z, y) =>
        {
            return z.GenerateNavigationPropertyLink(y, false);
        }, false);



        builder.Namespace = "WebApplication1.Models";

        return builder.GetEdmModel();
    }
is it suppose to work?
Jul 18, 2013 at 8:32 PM
Can you share your controller code and the request and the response as well?
Jul 18, 2013 at 8:36 PM
controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.OData;
using System.Web.Http.OData.Query;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
public class StoreController : ODataController//EntitySetController<Store, int>
{
    private static List<Store> _data = new List<Store>();

    static StoreController()
    {
        Store s = new Store()
        {
            ID = 1,
            Name = "1"
        };

        s.Products = new List<Product>();
        s.Products.Add(new Product()
            {
                ID = 1,
                Name = "1"
            });

        s.Products.Add(new Product()
        {
            ID = 2,
            Name = "2"
        });

        _data.Add(s);

        s = new Store()
        {
            ID = 2,
            Name = "1"
        };

        s.Products = new List<Product>();
        s.Products.Add(new Product()
        {
            ID = 2,
            Name = "1"
        });

        s.Products.Add(new Product()
        {
            ID = 3,
            Name = "2"
        });

        _data.Add(s);

    }

    [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    public  IQueryable<Store> Get()
    {
        return _data.AsQueryable();
    }


    public Store Get(int key)
    {
        return _data.FirstOrDefault(s => s.ID == key);
    }

    [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    public IQueryable<Product> GetProducts([FromODataUri] int key)
    {
        return _data.FirstOrDefault(s => s.ID == key).Products.AsQueryable();
    }
}
}

response:
{
"odata.metadata":"http://localhost:16894/odata/$metadata#Store","value":[
{
  "odata.type":"WebApplication1.Models.Store","odata.id":"http://localhost:16894/odata/Store(1)","Products@odata.navigationLinkUrl":"http://localhost:16894/odata/Store(1)/Products","Products":[
    {
      "odata.type":"WebApplication1.Models.Product","odata.id":"http://localhost:16894/odata/Product(1)","ID":1,"Name":"1"
    },{
      "odata.type":"WebApplication1.Models.Product","odata.id":"http://localhost:16894/odata/Product(2)","ID":2,"Name":"2"
    }
  ],"ID":1,"Name":"1"
},{
  "odata.type":"WebApplication1.Models.Store","odata.id":"http://localhost:16894/odata/Store(2)","Products@odata.navigationLinkUrl":"http://localhost:16894/odata/Store(2)/Products","Products":[
    {
      "odata.type":"WebApplication1.Models.Product","odata.id":"http://localhost:16894/odata/Product(2)","ID":2,"Name":"1"
    },{
      "odata.type":"WebApplication1.Models.Product","odata.id":"http://localhost:16894/odata/Product(3)","ID":3,"Name":"2"
    }
  ],"ID":2,"Name":"1"
}
]
}

request:
http://localhost:16894/odata/Store?$expand=Products
Jul 18, 2013 at 8:37 PM
this is the edm
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="WebApplication1.Models">
<EntityType Name="Product">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
</EntityType>
<EntityType Name="Store">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
<NavigationProperty Name="Products" Relationship="WebApplication1.Models.WebApplication1_Models_Store_Products_WebApplication1_Models_Product_ProductsPartner" ToRole="Products" FromRole="ProductsPartner"/>
</EntityType>
<Association Name="WebApplication1_Models_Store_Products_WebApplication1_Models_Product_ProductsPartner">
<End Type="WebApplication1.Models.Product" Role="Products" Multiplicity="*"/>
<End Type="WebApplication1.Models.Store" Role="ProductsPartner" Multiplicity="0..1"/>
</Association>
<EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
<EntitySet Name="Product" EntityType="WebApplication1.Models.Product"/>
<EntitySet Name="Store" EntityType="WebApplication1.Models.Store"/>
<AssociationSet Name="WebApplication1_Models_Store_Products_WebApplication1_Models_Product_ProductsPartnerSet" Association="WebApplication1.Models.WebApplication1_Models_Store_Products_WebApplication1_Models_Product_ProductsPartner">
<End Role="ProductsPartner" EntitySet="Store"/>
<End Role="Products" EntitySet="Product"/>
</AssociationSet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Jul 18, 2013 at 8:52 PM
I am not familiar with breeze enough but there doesn't seem to be an issue with web API here. As expected, Products property is expanded in the response. You might want to follow up with breeze folks. Jay Traband is responsive on stackoverflow.
Jul 24, 2013 at 8:01 PM
Unless you are using one of the MS OData model builder nightly builds, then this is unlikely to work. The "released" MS OData ModelBuilder does not support the definition of foreign keys which Breeze needs. Another poster pointed out that this has been fixed in one of the nightly builds, but we have not yet confirmed it. – Jay Traband yesterday

this is the reply i got
i have tried also with JayData - i guess that because both of them use datajs, it will not work
Jul 24, 2013 at 10:23 PM
We don't support defining foreign keys with ODataModelBuilder. I didn't realize that Breeze needs it for this scenario. We have recently enabled supporting EDM models that are created externally i.e not using ODataModelBuilder.

So, if you entity framework DBContext and want to use that EDM model instead (EF EDM model has foreign keys), you can now. See this for how to convert a DBContext to an EdmModel.
Aug 14, 2013 at 7:57 PM
will this be supported for ODataModelBuilder also? as i am working with the ODataModelBuilder and not using EF