当我的代码到达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);
}
希望这有助于 [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
@Sugandika我现在遇到“对象不能被删除,因为它没有在ObjectManager中找到”。我只有一个背景,所以没有意义? – Reinard 2012-04-03 09:26:47
@Sugandika请不要编辑问题以向OP添加消息。这是什么意见。 – 2012-04-03 09:28:22