2013-08-18 99 views
0

請跳過此閱讀編輯:)微風添加項目多對多我需要刷新頁面添加新項

當我有許多用於許多映射其他兩個實體的實體:

public class ExerciseAndCategory 
    { 

     #region Navigational properties 

     public int CategoryId { get; set; } 
     public virtual ExerciseCategory ExerciseCategory { get; set; } 


     public int ExerciseId { get; set; } 
     public virtual Exercise Exercise { get; set; } 

     #endregion 
    } 

這裏是FluentApi它:

modelBuilder.Entity<ExerciseAndCategory>().HasKey(e => new {e.ExerciseId, e.CategoryId}); 


      modelBuilder.Entity<ExerciseAndCategory>() 
       .HasRequired(e => e.Exercise) 
       .WithMany() 
       .HasForeignKey(e => e.ExerciseId) 
       .WillCascadeOnDelete(true); 

      modelBuilder.Entity<ExerciseAndCategory>() 
       .HasRequired(e => e.ExerciseCategory) 
       .WithMany() 
       .HasForeignKey(e => e.CategoryId) 
       .WillCascadeOnDelete(true); 

而且在數據庫中,看起來像這樣:

Database

要獲得並保存我使用BreezeApi控制器的數據,因爲它表明微風樣品中我添加數據:

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

並在服務(使用微風)我送數據API這樣的:

addExerciseAndCategories: function (data, initialValues) { 

      var addedExercise = manager.createEntity("Exercise", initialValues); 

      _.forEach(data, function (item) { 
       var entity = manager.createEntity("ExerciseAndCategory", { ExerciseId: addedExercise._backingStore.ExerciseId, CategoryId: item.CategoryId }); 
       items.push(entity); 

      }); 
      saveChanges().fail(addFailed); 

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

有了這個,我可以添加項目到數據庫,但如果我想補充而另一個沒有令人耳目一新,

我在這部分代碼得到異常:

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

DBUpdateException was unhandeled by user code 
An error occurred while updating the entries. See the inner exception for details. 

而在JavaScript控制檯:

"Error: Error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.ExerciseAndCategories_dbo.Exercises_ExerciseId". The conflict occurred in database "aspnet-Web.UserInterface.Web-20130806182934", table "dbo.Exercises", column 'ExerciseId'. 
The statement has been terminated." 

但是,如果我加入後刷新,一切工作正常。對於問題(即我可以添加另一個項目)

編輯

我發現非常糟糕的解決辦法。問題在於,在調用saveChanges()之前,ExerciseId是-1。我決定在兩種不同的功能中分別添加練習和類別。和我的代碼看起來是這樣的:

addExercise: function (initialValues) { 
      exerciseToAdd = manager.createEntity("Exercise", initialValues); 
      saveChanges().fail(addFailed); 

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

addExerciseAndCategories:功能(數據){ newItems = [];

 logger.log("id is", exerciseToAdd.ExerciseId); 
     _.forEach(data, function(item) { 
      var entity = manager.createEntity("ExerciseAndCategory", { ExerciseId: exerciseToAdd.ExerciseId, CategoryId: item.CategoryId }); 
      items.push(entity); 
      newItems.push(entity); 

     }); 
     saveChanges().fail(addFailed); 

     var itemsToAdd = addCategoriesToExercise(newItems); 

     _.forEach(itemsToAdd, function(item) { 
      exerciseAndCategories.push(item); 
     }); 
    } 

而這並沒有工作,同樣的錯誤。所以在控制檯中,我發現ExerciseId仍然是-1,然後我決定使用angular的$ timeout並等待幾秒鐘,然後記錄ExerciseId,之後ExerciseId的int大於-1,即它運行正常,所以我已經改變了代碼,這(和一切正常,因爲它應該):

addExerciseAndCategories: function(data) { 
      newItems = []; 

      $timeout(function() { 
       logger.log("id je", exerciseToAdd.ExerciseId); 
       _.forEach(data, function (item) { 
        var entity = manager.createEntity("ExerciseAndCategory", { ExerciseId: exerciseToAdd.ExerciseId, CategoryId: item.CategoryId }); 
        items.push(entity); 
        newItems.push(entity); 

       }); 
       saveChanges().fail(addFailed); 

       var itemsToAdd = addCategoriesToExercise(newItems); 

      _.forEach(itemsToAdd, function(item) { 
       exerciseAndCategories.push(item); 
      }); 
      },5000); 


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

最後,因爲我知道這是很糟糕的解決方案,我的問題是,我怎麼能解決這個得到什麼?有什麼方法可以讓我知道Exercise已插入數據庫,並且ExerciseId不再是-1了嗎?或者我可以在客戶端生成ExerciseId(int)嗎?

回答

0

Breeze尚未支持Entity Framework的多對多關係,儘管它在我們的路線圖上。請在Breeze User Voice here上投票。作爲一種解決方法,您可以將兩個一對多關係與作爲中間實體公開的映射表一起使用。

例如:如果你有一個概念上的許多到很多這樣的關係:

Employee (1) <--> (m) EmpTerritory (m) <--> Territory (1) 

Properties: Employee.EmployeeTerritories (m), 
      Territory.EmployeeTerritories (m), 
      EmployeeTerritory.Employee (1), 
      EmployeeTerritory.Territory (1) 

Employee (m) <--> (m) Territory 

Properties: Employee.Territories (m) 
      Territory.Employees (m) 

您可以通過在中間增加一個新的EntityType(EmpTerritory),而不是構建這您仍然可以從員工導航到其領土,反之亦然,但您需要先通過中間實體。

+0

謝謝,爲了答案,但不是我在做什麼?我在Exercise和Category中間暴露了實體ExerciseAndCategory,或者我做錯了? – hyperN

+0

如果你知道,請你能看到我的編輯和幫助嗎? – hyperN

+0

@JayTraband你能給我們一個如何使用這個工作的例子嗎? –