Breeze not getting metadata automatically

2019-02-28 04:34发布

问题:

The Breeze documentation says:

This [MetadataStore.FetchMetadata] call is made automatically by Breeze just before it attempts to perform it's first query against a remote service

But I am making a call like this:

function getBags() {

    var bags;
    var query = breeze.EntityQuery.from('Bags');

    return manager.executeQuery(query).then(querySucceded, _queryFailed);

    function querySucceded(data) {
        bags = data.result;
        logSuccess("Retrieved Bag Data")
        return bags;
    }
}

function _queryFailed(error) {
    logError(config.appErrorPrefix + "Query Failed: " + error.message);
    throw error;
}

When I do this I get this error:

"Unable to locate a 'Type' by the name: 'Bag:#DataAccess'. Be sure to execute a query or call fetchMetadata first."

as the value of error.message in _queryFailed.

NOTE: I know it is connecting to my OData service, because if I put in random stuff for my query I get a resource not found:

Resource not found for the segment 'Bagsasdfas'.;

I am using the HotTowel.Angular and HotTowel.Angular.Breeze nuget packages. The only difference is that I am connecting directly to a WCF Data Service (OData).

I do have these calls before I call the query:

breeze.config.initializeAdapterInstances({ dataService: "OData" });
breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);

Anyone got an idea why Breeze would not go get the metadata automatically for this?

回答1:

I found the fix for this issue. Breeze is getting the metadata.

But Breeze has one mostly undocumented OData Requirement that is not part of the OData spec:

In the metadata the Schema Namespace (found toward the top of the metadata) and the EntitySet EntityType namespace (found under EntityContainer) must be the same.

Thanks to the Pluralsight course by Brian Noyes for letting me know this.

NOTE: This is for WCF Data Services version of OData. Your mileage may vary for Web API OData.

I was able to get this working by making the project that held my Entity Framework model have the same name and namespace as the model's (edmx) namespace.

You can set the namespace of the edmx via right clicking in the designer and selecting properties (namespace is one of the properties).

Once I changed my project (and all the files in it) from the name "DataAccess" to the name of my edmx namespace Breeze started working just fine.



回答2:

I've submitted this bug for Breeze, which addresses this issue:

$metadata handling: entity types in separate schema from container not supported #96

I don't think it's correct that Breeze requires the EntityType namespace to match the Schema namespace - I think it's a bug.

The fix is a 1 line fix, which you can find here: https://github.com/johncrim/breeze.js/commit/9caa76a0de903e08083f600a2a23b9203ef87b49

You can use https://github.com/johncrim/breeze.js if you like, it only includes that patch on top of the current breeze.js master. But note that it's already 4 commits behind Breeze:master.