我正在尝试使用 Breeze 和 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 的一部分:
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
}
这是我的控制器的一部分:
$scope.addNewItem = function() {
var newItem = muscleGroupService.addItem({ Id: 42, Name: $scope.Name, Description: "ho", ImageUrl: "hey"});
$scope.items.push(newItem);
};
这是我得到的错误:
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
在尝试添加 antoher 之前,我会查询所有肌肉组。