Search code examples
c#angularjsentity-frameworkasp.net-web-apibreeze

Angular/Breeze SPA loading local data from webAPI service to unmapped entity


I'm following John Papa's brilliant pluralsight course on AngularJS/Breeze SPA apps (part 1) and making mods to my particular situation.

I have reference data for Products and Uoms, which are meant for dropdown lists. I want to load these from breeze local cache versus hitting the server all the time. I can retrieve them from the server, but I can't figure out how to query them later from breeze.

The data is exposed server-side as lists of id/name pair objects which are not part of the metadataStore exposed by Entity Framework. I add these entity types to breeze before the call to the server for data.

Here's how I created the unmapped EntityTypes:

  createReferenceEntity('Products');
  createReferenceEntity('Uoms');

  function createReferenceEntity(entityName) {

    // add reference/lookup client-side only entity type
    var et = new breeze.EntityType({
      shortName: entityName,
      namespace: "Model"
    });

    et.addProperty(new breeze.DataProperty({
      name: "id",
      dataType: breeze.DataType.Int32,
      isNullable: false,
      isPartOfKey: true,
    }));

    et.addProperty(new breeze.DataProperty({
      name: "name",
      dataType: breeze.DataType.String,
      isNullable: false,
    }));

    metadataStore.addEntityType(et);
  }

Here's what I have in my Breeze controller:

    [HttpGet]
    public object Lookups()
    {
        // List<ReferenceObject> which is an Id and a Name
        var Products = _repository.Products; 
        var Uoms = _repository.Uoms;
        return new { Products, Uoms };
    }

So I call into the controller from javascript with:

    return EntityQuery.from('Lookups').using(manager)
    .execute().then(querySucceeded, _queryFailed);

The query succeeds. What I see in the FireBug Net window looks to be ok. So the data is coming across the wire.

So what I cannot seem to be able to do is to then query the lookup data again. Is it not supposed to be in the Breeze cache? This is what I am trying. Comes back as empty:

    _getReferenceData('Products', 'name');

    function _getReferenceData( resource, ordering) {
      return EntityQuery.from(resource)
             .orderBy(ordering)
             .using(manager)
             .executeLocally();
    }

Maybe the server is not providing the serialized object across the wire in the right format? I'm not querying breeze properly?

I'd really appreciate any advice here, as I'm getting little bleary eyed trying to figure this out.

Thanks! Corey.


Solution

  • I think you need to call setEntityTypeForResourceName:

    metadataStore.setEntityTypeForResourceName("Products", "Product:#Model");
    metadataStore.setEntityTypeForResourceName("Uoms", "Uom:#Model");
    

    Then the EntityManager can know that, when you use the "Products" resource name, it should query "Product" entities. Normally Breeze gets this information from the metadata returned by the server, but since you create the EntityTypes locally, you'll need to provide the info.