breezejs: navigation property is created but not f

2019-02-27 01:57发布

问题:

I'm having yet another issues with navigation properties but this time my configuration is WCF Data Service + EF.

Basically the metadata looks good, I have the referential constraints, association, etc... I've set the [ForeignKey] attribute in the model.

The navigation property is created on the client-side, but when data is retrieved (using $expand), the collection is not filled although data is definitely returned by the server :

The association here is between mandate_id on OpenPosition and id on Mandate.

I've noticed that the Mandate entity in the OpenPositions collection contains __deferred with the uri to the entity. I had not seen that with WebApi so maybe that's the problem, or maybe it's just normal behaviour.

Is there something I'm missing ?

EDIT

The client-side query is :

  var query = breeze.EntityQuery.from("Mandates").inlineCount().expand("OpenPositions");
  return manager.executeQuery(query.using(service)).then(function (result) {
        logger.info(result);
    }).fail(function (error) {
        logger.error(error);
    });

EDIT 2

The reason the navigation property is not filled is because in the case of WCF Dataservice, the navigation property is returned as an object, which contains an Array property called results.

Whereas in the case of WebAPI, the navigation property is returned as an Array.

Note that in both cases, the same data model (EF context) was used.

See screenshots:

WCF :

WebAPI:

But breeze expects an Array otherwise, it just ignores the navigation property and returns null:

   // needed if what is returned is not an array and we expect one - this happens with __deferred in OData.
        if (!Array.isArray(relatedRawEntities)) return null;

回答1:

Ok, FINALLY it's working :) I am now using the latest commit of breezejs on github and this fixed my problem. In my opinion breeze via WCF never worked until now with ODATA2. The ODATA specification is clear about that:

  • ODATA V1 : collections are represented as an array
  • ODATA V2: collections are represented as an object containing an array.

The only way I could get it to work before using the github version of breezejs, was by specifying MAX_DATA_SERVICE_VERSION to 1.0 in datajs.

Anyway, that's all good now. I can't wait for the breeze release :) By the way it's likely I'm going to ditch OpenAccess for NHibernate. Is the NH WebAPI controller as complete as the EF WebAPI controller ?



回答2:

Are you using the "WebApi" dataservice adapter or the "OData" data service adapter? In general, you should be using the "webApi" dataservice adapter unless you are explicitly exposing your data via OData. The "webApi" adapter is the default so I'd try just removing any "initializeAdapterReference" calls that mention "OData".



回答3:

As a follow up, the issue is just that:

-with classic ODATA service, navigation properties are embedded in an object called 'results'.

:

Whereas with WebAPI service, navigation properties are just an array.

Then at some point in breeze code there's the following test:

 // needed if what is returned is not an array and we expect one - this happens with __deferred in OData.

if (!Array.isArray(relatedRawEntities)) return null;

and in the case of classic ODATA service, it obviously returns null and navigation property is not populated.

I don't know how to make myself any clearer than that. It does not look like a problem with my metadata, but more like a bug in breeze.

Can you confirm that what I'm saying makes sense ? Can we do something about that ?