0
votes

I am "successfully" executing a breeze query against an asp.net web api ODATA service, but there doesn't seem to be any children, even though the response has the children in it.

query: var query = breeze.EntityQuery.from('Transactions') .take(20) .orderBy('Transaction_Timestamp desc') .expand('Items');

How do I access the children? I have tried .Items, .Items(), .Item, .Item(), there is no property or method there. On the service side, I verified that there are navigation properties from Transaction to Items (Transaction.Items), and Items.Transaction. And, as I said, the http response from the service has all of the children's data.

Where can I look to see where the problem might be?

Breeze config:

    breeze.config.initializeAdapterInstance('dataService', 'webApiOData', true);

    $http.defaults.headers.put = {
        'Access-Control-Allow-Origin': '*'
    };

    new breeze.ValidationOptions({ validateOnAttach: false }).setAsDefault();

    var metadataStore = createMetadataStore();
    etc.

My Transaction class:

 public partial class Transaction
    {
        public Transaction()
        {
            this.Discounts = new HashSet<Discount>();
            this.Items = new HashSet<Item>();
            this.Tenders = new HashSet<Tender>();
        }

        public System.Guid Id { get; set; }
       ...

        public virtual ICollection<Item> Items { get; set; }

    }

My Item class:

public partial class Item
{
    public System.Guid Id { get; set; }
    public System.Guid Transaction_Id { get; set; }

   ...

    public virtual Transaction Transaction { get; set; }
}

Would this not be enough?

odata/$metadata for Item entity:

<EntityType Name="Item">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Guid" Nullable="false"/>
<Property Name="Transaction_Id" Type="Edm.Guid" Nullable="false"/>
... (omitted fields)

<NavigationProperty Name="Transaction" Relationship="TransactionService.Models.TransactionService_Models_Item_Transaction_TransactionService_Models_Transaction_TransactionPartner" ToRole="Transaction" FromRole="TransactionPartner"/>
</EntityType>

This is data returned in the Transactions.Expand('Items') breeze query:

{
  "odata.metadata":"http://localhost:49858/odata/$metadata#Transactions","value":[
    {
      "odata.type":"TransactionService.Models.Transaction","odata.id":"http://localhost:49858/odata/Transactions(guid'f93805cf-cc80-4d2e-9d9e-97df9c21c622')","[email protected]":"http://localhost:49858/odata/Transactions(guid'f93805cf-cc80-4d2e-9d9e-97df9c21c622')/Discounts","[email protected]":"http://localhost:49858/odata/Transactions(guid'f93805cf-cc80-4d2e-9d9e-97df9c21c622')/Shift","[email protected]":"http://localhost:49858/odata/Transactions(guid'f93805cf-cc80-4d2e-9d9e-97df9c21c622')/Tenders","[email protected]":"http://localhost:49858/odata/Transactions(guid'f93805cf-cc80-4d2e-9d9e-97df9c21c622')/Items","Items":[
        {
          "odata.type":"TransactionService.Models.Item","odata.id":"http://localhost:49858/odata/Items(guid'511348d7-2886-4b13-ad7e-0eeb2a11bd85')","[email protected]":"http://localhost:49858/odata/Items(guid'511348d7-2886-4b13-ad7e-0eeb2a11bd85')/Transaction","[email protected]":"Edm.Guid","Id":"511348d7-2886-4b13-ad7e-0eeb2a11bd85","[email protected]":"Edm.Guid","Transaction_Id":"f93805cf-cc80-4d2e-9d9e-97df9c21c622","[email protected]":"Edm.DateTime","Updated":"2014-05-06T20:30:30.657","[email protected]":"Edm.Int16","Ordinal":1,"Item_Type":"item      ","Item_Number":"39                  ","Parent_Ordinal":null,"[email protected]":"Edm.Byte","Modifier":0,"Description":"Cinnamon Coffee Cake","Item_Serial":null,...

etc...

3
Can you show your models and their relationships? Remember that in your model definitions you need to have a proper relationship between the two models so that Breeze knows when Items show up in the JSON that it is of type Item to map it properly.PW Kad
I may not get your questions right. So your point is: you can get the right response, but you cannot read the expanded items out with Breeze Api?zoe

3 Answers

0
votes

Can you show us your Transaction and Items model classes? I bet you are missing a foreign key there...

Note that just setting the navigation property in your model will not suffice for Breeze to detect the association.

Example:

    //This will not work

    public class Transaction
    {
         public Transaction()
         {
            Items = new HashSet<Items>();
         }
         public int Id {get;set;}
         public virtual ICollection<Items> Items {get;set;}
    }

    public class Items
    {
       public int Id {get;set;}
       public Transaction Transaction {get;set;}
    }

Everything looks ok, right? Wrong.

Why?

Because the Items class doesn't have a FK property towards the parent. You'd need to modify the class to look like this:

   public class Items
   {
       public int Id {get;set;}
       public int TransactionId {get;set;} // FK Property

       public Transaction Transaction {get;set;}
   }

Not saying this is your problem, but you should confirm that you have FK Properties set properly in order to Breeze work.

Here's why: http://www.breezejs.com/documentation/navigation-properties#foreignkeys

0
votes

Ok, the first part of debugging this requires that you investigate the metadata in the local EntityManager immediately after a EntityManager.fetchMetadata call. ( This call occurs automatically with the first query, but can also be called independently BEFORE the first query.)

My guess is that the metadata that you are getting from the server is not complete in terms of its definition of the 'Items' property. You can check this AFTER the EntityManager.fetchMetadata call by drilling into your 'Transaction' EntityType. Something like this:

 myEntityManager.fetchMetadata().then(function () {
     var transactionType = myEntityManager.metadataStore.getEntityType("Transaction");
     var itemsProperty = transactionType.getProperty("Items"); 
     // if itemsProperty is null; then it is likely that your server side model is not correct.
 });

Please post back when you have the result of this test, and we may be able to determine more.

0
votes

Once I changed all of my model to remove all underscores, things worked much better. I would recommend sticking with no underscores in column or table names. They don't seem to be a problem in the foreign key names.

Also, I was not using the Breeze EdmBuilder model builder on the service side.