Question

I have client-side defined metadata, like so:

helper.addTypeToStore(manager.metadataStore, {
  name: 'PriorStudy',
  dataProperties: {
    priorStudyId: { type: breeze.DataType.Int32 },
    priorStudyType: { max: 6 },
    priorStudyPurpose: { max: 12 },
    notes: { max: 250 }
  }
});

helper.addTypeToStore(manager.metadataStore, {
  name: 'Patient',
  dataProperties: {
    patientId: { type: breeze.DataType.Int32 },
    firstName: { max: 25 },
    lastName: { max: 25 },
  },
  navigationProperties: {
    priorStudies: { entityTypeName: 'PriorStudy', fk: ['patientId'], hasMany: true}
  }
})    

My JSON response from the server is shaped like this:

{ FirstName: "Steve",
  LastName: "Holt",
  PatientId: 1,
  PriorStudy: [
    {
      PriorStudyId: 1,
      PriorStudyType: "BLAH",
      PriorStudyPurpose: "Whatever",
      PatientId: 1,
      Notes: "la la la"
    }
  ]
}

I'm testing the creation of entities like so:

var query = breeze.EntityQuery.from('Patient/1').toType('Patient')
console.log(query);
manager.executeQuery(query).then(function (results) {
  console.log(results);
})
.then(function () {
  var ents = breeze.EntityQuery.from('PriorStudy').using(manager).executeLocally();
  console.log(ents);
});

The Patient entity is created as expected, but no matter what I attempt the PriorStudy entity is empty. I'm about to resort to writing a JsonResultsAdapter.

I know this sounds a lot like other questions, but I've been banging my head against it for two days and don't know what else to try. Any advice would be tremendously appreciated.

EDIT: Link to non-working code https://gist.github.com/dlmanning/c09fe225995bc7cb682b

Was it helpful?

Solution

You are missing a few things here.

1 - You're redefining your client side metadata incorrectly. The correct definition for your 'PriorStudy' entity type should look like this.

    name: 'PriorStudy',
    dataProperties: {
       priorStudyId: { type: breeze.DataType.Int32 },
       patientId: { type: breeze.DataType.Int32 },
       priorStudyType: { max: 6 },
       priorStudyPurpose: { max: 12 },
       notes: { max: 250 }
    },

    navigationProperties: {
      patient: 'Patient',
    }

Notice the 'patientId' foreign key as well as the 'patient' scalar navigation property.

Then the definition for your 'Patient' entity type can look as follows.

    name: 'Patient',
    dataProperties: {
      patientId: { type: breeze.DataType.Int32 },
      firstName: { max: 25 },
      lastName: { max: 25 },
    },

    navigationProperties: {
      priorStudies: { entityTypeName: 'PriorStudy', hasMany: true }
    }

Notice that you don't specify the 'priorStudies fk' property on the 'Patient' entity type.

2 - When you have defined the metadata correctly, then in your query, you can query your Patient with the 'expand' method to also bring the 'PriorStudies' from the database.

var query = breeze.EntityQuery.from('Patient/1') .toType('Patient') .expand('priorStudies');

EDIT 1:

Adding Plunkr

EDIT 2:

In addition to defining (and fixing) the client-side metadata, you also have to write your own custom JsonResultsAdapter. Look for the Edmunds and ESPN samples on the Breeze Samples page. The Breeze mapping json tutorial, may also prove to be helpful.

Here's a gist that I've created to illustrate how to write a custom jsonResultsAdapter. Look for the createJsonResultsAdapter method.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top