Foreign key constraint, EF with collection of childobjects

后端 未结 4 1392
遥遥无期
遥遥无期 2021-01-27 09:31

I\'m trying to update a model, but get the error \"The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable.

相关标签:
4条回答
  • 2021-01-27 09:39

    One thing of note is that you shouldn't have the [Required] on Customer as its inferred from the fact that your FK isn't nullable. Required should only be used on a navigation property if you do not have the FK in the model.

    To try to diagnose the issue, can you load the object and look at it in a debugger, you should expect that both locationId and CustomerId have non-zero values.

    0 讨论(0)
  • 2021-01-27 09:43

    can you add the code for your event object. The one you call original.

    It might be so that the UpdateModel change some info on the associated objects and that's not good if so. Not sure about this though I can't see all the code.

    I prefer to not uder UptadeModel and instead use a inputmodel or your MVC model as the inparameter and manually map the chages to the loaded original object.

    Antoher problem is that I can't see if eventRepository.Save();

    really do an SaveShages? does it? I can se some context code in another method Save?

    0 讨论(0)
  • 2021-01-27 09:54

    I found a solution to my problem. It seems to be a bug (?) in ASP.NET MVC when it comes to UpdateModel and a model containing an ICollection.

    The solution is to override the default behaviour, as described in this blog post: http://www.codetuning.net/blog/post/Binding-Model-Graphs-with-ASPNETMVC.aspx

    Update I found a solution! The above only worked when updating existing items in the collection. To solve this, I have to manually check and add new AttendeeInformationFields. Like this:

    [HttpPost]
    public ActionResult Edit(int id, MagnetEvent magnetEvent)
    {
        var eventRepository = new MagnetEventRepository();
        var original = eventRepository.Find(id);
        UpdateModel(original);
        foreach (var attendeeInformationField in magnetEvent.CaptureAttendeeInformationFields)
        {
            var attendeeInformationFieldId = attendeeInformationField.Id;
            if (original.CaptureAttendeeInformationFields.AsQueryable().Where(ai => ai.Id == attendeeInformationFieldId).Count() == 0)
            {
                original.CaptureAttendeeInformationFields.Add(attendeeInformationField);
            }
        }
        eventRepository.Save();
    }
    

    Together with the modified DefaultModelBinder, this actually works with both editing and adding. For now I haven't tried deleting.

    Still, I hope there is a simpler way to do this. Seems like a lot of coding to do a very basic task.

    0 讨论(0)
  • 2021-01-27 09:59

    As the exception say it seams like your associated collections or other associated objects cant find a valid ID value.

    Are you Eager-loading the associated objects? like Customer?

    0 讨论(0)
提交回复
热议问题