GETah
GETah

Reputation: 21409

Breeze fails to resolve the web api controller method

I am using Breeze on both my client and server with a sql express database.

My server code :

[Authorize]
[BreezeController]
public class DataController : ApiController{

   [HttpGet]
   public IQueryable<Product> Products()
   {
     ...
   }
}

My client code:

var getProducts = function () {
        var query = entityQuery.from('Products');

        return manager.executeQuery(query)
               .then(querysucceeded)
               .fail(queryfailed);
        ...
    };

This works fine. However, the documentation says that the parameter passed to EntityQuery.from(...) should be one of the breeze web api controller methods. So I would expect adding the following to the server:

   [HttpGet]
   public IQueryable<Product> TestMethod()
   {
     ...
   }

And this to the client:

var getProjects = function () {
        var query = entityQuery.from('TestMethod');

        return manager.executeQuery(query)
               .then(querysucceeded)
               .fail(queryfailed);
        ...
    };

I expected this to work but it doesn't :( I get the following error on the console:

Uncaught Error: Cannot find an entityType for resourceName: 'TestMethod'. Consider adding an 'EntityQuery.toType' call to your query or calling the MetadataStore.setEntityTypeForResourceName method to register an entityType for this resourceName.

Upvotes: 1

Views: 1239

Answers (1)

Jay Traband
Jay Traband

Reputation: 17052

The Breeze client needs to know the EntityType that is being returned corresponding to each "resourceName". It knows this for the "Products" resource because of Metadata, where this resourceName is paired with the "Product" entityType, but it has no idea what "TestMethod" returns.

So your fix is to either call

manager.setEntityTypeForResourceName("TestMethod", "Product");

before executing your query. Or you can change your query to use the 'toType' method.

var query = entityQuery.from('TestMethod').toType("Product");
return manager.executeQuery(query)

Upvotes: 4

Related Questions