2012-04-03 87 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