2012-04-03 35 views
0

當我的代碼到達CampaignRepository.saveChanges()時,它提供了錯誤,並且關係無法修改,但關鍵屬性不能爲空。但是如果我調試它並查看當前對象的內容,則所有主鍵和外鍵都被設置。我不知道爲什麼它會錯...ASP.NET MVC EF代碼優先:關係無法修改

public ActionResult Update(LandingPage update) 
    { 
     Campaign curr = CampaignRepository.FindById(update.CampaignId); 
     curr.UpdatePage(update); 
     CampaignRepository.SaveChanges(); 
     return View("Edit", curr); 
    } 

public void UpdatePage(LandingPage update) 
    { 
     foreach (Page page in Pages) 
     { 
      if (page.Id == update.Id) 
      { 
       Pages.Remove(page); 
       break; 
      } 
     } 
     Pages.Add(update); 
    } 

德relatie根Niet的沃登gewijzigd,米爾麪包車去的omdat VOOR EEN referentiële-sleuteleigenschappen吉恩空waarde是toegestaan 。 相關文章:阿爾斯德 referentiëlesleutel空waarden Niet的ondersteunt,酩悅爾EEN NIEUWE relatie沃登gedefinieerd,酩去referentiële-sleuteleigenschap 沃登toegewezen AAN EEN ANDERE waarde模具Niet的空是moeten HET Niet的-gerelateerde對象沃登verwijderd的。


更新

我改變了我的更新方法:

public ActionResult Update(LandingPage update) 
     { 

      Campaign curr = Campaignrepository.FindById(update.CampaignId); 
      PageRepository.Remove(update); 
      curr.Pages.Remove(update); 
      curr.Pages.Add(update);       
      Campaignrepository.SaveChanges(); 

      return View("Edit", curr); 
     } 

但現在它說: 「對象不能被刪除,因爲它不是在的ObjectManager發現」。


更新2

不過 「的對象不能被刪除,因爲它不是在的ObjectManager發現」。

//A campaign has Pages property which is a collection of Pages 
//This collection contains a two (no more, no less) objects a LandingPage and a RedeemPage 
//both LandingPage and RedeemPage have as base Page 
//The objective is to replace the old LandingPage with the new version 
public ActionResult Update(LandingPage update) 
{ 
    //Get the campaign it's about 
    Campaign curr = Campaignrepository.FindById(update.CampaignId); 
    //Set the current Page his foreign key values to null 
    curr.GetLanding().Campaign = null; 
    curr.GetLanding().CampaignId = 0; 
    //Remove the (current) page from the repository/context 
    PageRepository.Remove(update); 
    //Remove the (current) page from the Campaign collection 
    curr.Pages.Remove(update); 
    //Add the new version of the page 
    curr.Pages.Add(update);  
    //Save the chances => ObjectManager error   
    PageRepository.SaveChanges();   
    return View("Edit", curr); 
} 
+0

希望這有助於 [http://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed - 因爲酮或者更多的最外鍵親] [1] [1]:http://stackoverflow.com/questions/5538974/the-relationship-could-不可改變,因爲一個或多個外鍵key-pro – Sugandika 2012-04-03 09:14:26

+0

@Sugandika我現在遇到「對象不能被刪除,因爲它沒有在ObjectManager中找到」。我只有一個背景,所以沒有意義? – Reinard 2012-04-03 09:26:47

+0

@Sugandika請不要編輯問題以向OP添加消息。這是什麼意見。 – 2012-04-03 09:28:22

回答

1

你可以試着改變UpdatePage方法:

public void UpdatePage(LandingPage update) 
{ 
    var pageToUpdate = this.Pages.Where(p => P.Id == update.Id).SingleOrDefault(); 

    if(pageToUpdate != null) 
    { 
     // update all properties of old page with new values 
     pageToUpdate.Title = update.Title; // for any property of Page 
    } 
} 
+0

如果我需要添加更多屬性>。<,但它的工作原理,謝謝。 – Reinard 2012-04-04 07:18:25

1

必須手動將舊的子項目從父級活動中逐個刪除。實體框架不這樣做。您必須決定您想要對舊的子項目做什麼 - 將其丟棄或保留它們,並將它們分配給其他父實體。你必須告訴EF你的決定。如果沒有引用數據庫中的任何父項(由於外鍵約束),子實體不能獨自生活。

關於異常,「對象不能被刪除,因爲它沒有在ObjectManager中找到」,要排序它,你必須將子對象的外鍵值設置爲0.下面是一個例子,如下所示。 (假設你的父對象是Campaign myCampaign)

myCampaign.ChildCampaign = null; 

必須完成。除了你必須做出的FK ID爲0

myCampaign.ChildCampaignId = 0; 

我與我以前的項目相同的問題,並能解決它做同樣的這一點。希望這可以幫助。

+0

沒有工作 curr.GetLanding()。Campaign = null; PageRepository.Remove(update); curr.Pages.Remove(update); curr.Pages.Add(update); PageRepository.SaveChanges(); – Reinard 2012-04-03 09:42:22

+0

一個小小的說明,在刪除'update'對象之前,你能在程序中做這樣的事嗎? curr.GetLanding()。Campaign = null; curr.GetLanding()。CampaignId = 0; – Sugandika 2012-04-03 09:45:35

+0

我將它設置爲null,然後嘗試從存儲庫中刪除它。但是在刪除它時,我仍然得到ObjectManager錯誤。編輯OP。 – Reinard 2012-04-03 10:00:22