Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC EF Code first: Relation can not be modified
    text
    copied!<p>When my code reaches CampaignRepository.saveChanges() it gives and error that the relation can not be modified, key properties can not be null. But if I debug it and look into the contents of the current object all primary keys and foreign keys are set. I have no clue why it's going wrong...</p> <pre><code>public ActionResult Update(LandingPage update) { Campaign curr = CampaignRepository.FindById(update.CampaignId); curr.UpdatePage(update); CampaignRepository.SaveChanges(); return View("Edit", curr); } </code></pre> <hr> <pre><code> public void UpdatePage(LandingPage update) { foreach (Page page in Pages) { if (page.Id == update.Id) { Pages.Remove(page); break; } } Pages.Add(update); } </code></pre> <blockquote> <p>de relatie kan niet worden gewijzigd, omdat voor een of meer van de referentiële-sleuteleigenschappen geen null-waarde is toegestaan. Wanneer een relatie wordt gewijzigd, wordt de gerelateerde referentiële-sleuteleigenschap ingesteld op een null-waarde. Als de referentiële sleutel null-waarden niet ondersteunt, moet er een nieuwe relatie worden gedefinieerd, moet de referentiële-sleuteleigenschap worden toegewezen aan een andere waarde die niet null is of moeten het niet-gerelateerde object worden verwijderd.</p> </blockquote> <hr> <p><strong>Update</strong></p> <p>I changed my Update method to:</p> <pre><code> 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); } </code></pre> <p>But now it says "The Object can not be removed because it's not found in the ObjectManager".</p> <hr> <p><strong>Update 2</strong></p> <p>Still "The Object can not be removed because it's not found in the ObjectManager". </p> <pre><code> //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 =&gt; ObjectManager error PageRepository.SaveChanges(); return View("Edit", curr); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload