title: Breeze: Unable to locate a 'Type' by the name: 'Person'. Be sure to execute a query or call fetchMetadata first
body: <p>I have downloaded <code>HotTowel.Angular.Breeze</code> from NuGet and set it up with a breeze api controller.</p>
  
  <p>I have configured the <code>datacontext.js</code> to create a new manager and have created and exposed a function to create a new entity:</p>
  
  <pre><code>function datacontext(emManager) {
      var manager = emManager.newManager();
  
      var service = {
          create: create,
          save: save
      };
  
      return service;
  
      function create(entityName) {
          return manager.createEntity(entityName);
      }
  
      function save() {
          return manager.saveChanges().to$q(saveSucceeded, saveFailed);
  
          function saveSucceeded(result) {
              // Save Successful
          }
  
          function saveFailed(error) {
              // Save Failed
          }
      }
  }
  </code></pre>
  
  <p>I then call this in my controller like so:</p>
  
  <pre><code>    function getNewPerson() {
          vm.person = datacontext.create(entityName);
          return vm.person;
      }
  </code></pre>
  
  <p>If I load this page directly in my browser, I get the following error:</p>
  
  <pre><code>Error: Unable to locate a 'Type' by the name: 'Person'. 
  Be sure to execute a query or call fetchMetadata first.
  </code></pre>
  
  <p>However, if I navigate to the page after executing a Query, it loads fine and I'm able to save the new entity etc..</p>
  
  <p>So my question is, where would be a good place to call <code>fetchMetadata</code>? Would it be in the <code>entityManagerFactory</code> on the <code>newManager</code> method?</p>
  
  <pre><code>    function newManager() {
          var mgr = new breeze.EntityManager({
              serviceName: serviceName,
              metadataStore: metadataStore
          });
          return mgr;
      }
  </code></pre>
  
  <h2>Edit</h2>
  
  <p>I've tried as suggested by @Rob below, but I still get the same error:</p>
  
  <pre><code>    function setupMetadata() {
          var deferred = $q.defer();
          metadataStore.fetchMetadata(serviceName).then(function () {
              deferred.resolve();
          }).fail(function (ex) {
              deferred.reject(ex);
          });
  
          return deferred.promise;
      }
  </code></pre>
  
  <p>Then I place <code>setupMetadata();</code> inside <code>app.run</code>:</p>
  
  <pre><code>app.run(['$route', '$rootScope', '$q', 'entityManagerFactory', function ($route, $rootScope, $q, emFactory) {
      // Include $route to kick start the router.
      breeze.core.extendQ($rootScope, $q);
  
      emFactory.setupMetadata();
  }]);
  </code></pre>