无法通过名称来查找

无法通过名称来查找

本文介绍了微风错误:无法通过名称来查找'类型':'' - 首先调用fetchMetadata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想新项目保存到使用微风和Web API我的数据库。

I'm trying to save new item to my database using Breeze and web api.

下面是我的模型:

 public class MuscleGroup
    {

        #region Properties

        public int MuscleGroupId { get; set; }

        public string Name { get; set; }

        public string Description { get; set; }

        public string ImageUrl { get; set; }
        #endregion

        #region Navigational properties

        public virtual ICollection<Muscle> Muscles { get; set; }

        public virtual ICollection<Exercise> Exercises { get; set; }



        #endregion
    }

下面是我的API:

[BreezeController]
    public class MuscleGroupController : ApiController
    {
        private readonly EFContextProvider<MadBarzDatabaseContext> _contextProvider =
            new EFContextProvider<MadBarzDatabaseContext>();


        // GET api/<controller>
        [HttpGet]
        public IQueryable<MuscleGroup> Get()
        {
            return _contextProvider.Context.MuscleGroups;
        }

        [HttpPost]
        public SaveResult SaveChanges(JObject saveBundle)
        {
            return _contextProvider.SaveChanges(saveBundle);
        }
    }

下面是我dataService.js的部分:

Here are parts of my dataService.js:

app.angularModule.service('muscleGroupService', function(breeze, logger) {


    breeze.config.initializeAdapterInstance("modelLibrary", "backingStore", true);

    var mbservice = new breeze.DataService({
        serviceName: "http://localhost:23758/api/MuscleGroup",
        hasServerMetadata: false,
    });

    var manager = new breeze.EntityManager({ dataService: mbservice });

    manager.enableSaveQueuing(true);

    var removeItem = breeze.core.arrayRemoveItem;

    var items = [];

    var muscleGroupService =
    {
        getAll: getAll,
        getSucceeded: getSucceeded,
        getFailed : getFailed,
        addItem: addItem,
     //   deleteItem: deleteItem,
     //   updateItem : updateItem,
    };

    return muscleGroupService;


    //#region addItem

    function addItem(initialValues) {
        var item =  manager.createEntity("MuscleGroup", initialValues);
        saveChanges().fail(addFailed);
        items.push(item);
        return item;

        function addFailed() {
            removeItem(items, item);
        }
    }

    //#endregion

    //#region SaveChanges
    function saveChanges() {
        return manager.saveChanges()
           .then(saveSucceeded)
           .fail(saveFailed);
    }

    function saveSucceeded(saveResult) {
        logger.success("Saved :D");
        logger.log(saveResult);
    }

    function saveFailed(error) {
        logger.error(error);
        logger.log(error);
    }
    //#endregion
}

下面是我的控制器的一部分:

Here is part of my controller:

$scope.addNewItem = function() {
        var newItem = muscleGroupService.addItem({ Id: 42, Name: $scope.Name, Description: "ho", ImageUrl: "hey"});
        $scope.items.push(newItem);
    };

这是错误我得到:

And this is error I get:

Error: Unable to locate a 'Type' by the name: 'MuscleGroup'. Be sure to execute a query or call fetchMetadata first.
    at proto._getEntityType (http://localhost:7122/Scripts/breeze.debug.js:6056:19)
    at proto.getEntityType (http://localhost:7122/Scripts/breeze.debug.js:6047:21)
    at Object.addItem (http://localhost:7122/Scripts/app/AdminMuscleGroup/MuscleGroupService.js:61:45)
    at Object.$scope.addNewItem (http://localhost:7122/Scripts/app/AdminMuscleGroup/MuscleGroupController.js:16:42)
    at http://localhost:7122/Scripts/angular/angular.js:6365:19
    at http://localhost:7122/Scripts/angular/angular.js:12987:13
    at Object.Scope.$eval (http://localhost:7122/Scripts/angular/angular.js:8057:28)
    at Object.Scope.$apply (http://localhost:7122/Scripts/angular/angular.js:8137:23)
    at HTMLButtonElement.<anonymous> (http://localhost:7122/Scripts/angular/angular.js:12986:17)
    at HTMLButtonElement.jQuery.event.dispatch (http://localhost:7122/Scripts/jquery-1.8.2.js:3063:9) angular.js:5754

我查询所有Muslce组之前,我尝试添加antoher。

I query All Muslce groups before I try to add antoher.

推荐答案

PW喀是给你线索。

当你写 hasServerMetadata:假,你叫清风不从服务器获取元数据;你告诉微风,你会在客户端中的元数据。你是不是在客户端上提供的元数据。

When you wrote hasServerMetadata: false, you told Breeze not to get metadata from the server; you told Breeze that you would provide the metadata on the client. You aren't providing metadata on the client.

您是控制器不具有元数据端点...并不能提供这样的端点和另被构造为控制器每次型的API你似乎决心控制器设计。

You're controller doesn't have a Metadata endpoint ... and couldn't offer such an endpoint AND ALSO be structured as the controller-per-type API controller that you seem determined to devise.

您是从标准的微风生产力路径,而追求控制器每类偏离。这很好......你有一点点的经验后,知道自己在做什么,知道你为什么要这么做。在 hasServerMetadata 标志为true在默认情况下是有原因的;你不能只将其设置为false,并期望一切工作。

You're deviating from the standard Breeze productivity path while pursuing controller-per-type. That's fine ... after you have a little experience, know what you're doing, and know why you're doing it. The hasServerMetadata flag is true by default for a reason; you can't just set it false and expect everything to work.

我建议你回涨,跟随微风指导,了解它,然后从该指导意见增量当您发现这样做扎实,业务的原因。

I suggest that you back up, follow the Breeze guidance, understand it, then depart from that guidance incrementally as you discover solid, business reasons for doing so.

这篇关于微风错误:无法通过名称来查找'类型':'' - 首先调用fetchMetadata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:48