Foreign key constraint, EF with a set of child objects

I try to update the model, but I get the error message “Operation failed: the relation cannot be changed because one or more properties of the foreign key are not NULL. When the relation changes, the corresponding property of the foreign key is set to zero. If the foreign key doesn’t support null values, a new relationship needs to be defined, another nonzero value must be assigned to the foreign key property, or an unrelated object must be deleted. "

From what I understand from Relationships cannot be changed, since one or more properties of the foreign key cannot be nullified , the problem may be due to the way the Entity Framework handles my virtual ICollection

However, I'm not sure how to implement the solution when using the forest repository template. Do I need to edit the Save () class - method ParentObjectRepository-class?

Actually, I really think there must be some way to get EF to understand this. I don’t see what the EF team thought: "Probably no one uses a collection of objects with foreign key constraints, doesn’t support this."

Update Code Added

[HttpPost] public ActionResult Edit(int id, FormCollection formCollection) { var eventRepository = new MagnetEventRepository(); var original = eventRepository.Find(id); UpdateModel(original); eventRepository.Save(); return RedirectToAction("Details", "Home", new { slug = original.Slug }); } public void Save() { context.SaveChanges(); } 

More code:

 public class MagnetEvent { public virtual int Id { get; set; } [Required] public virtual string Name { get; set; } [Required] [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm}")] [DataType(DataType.DateTime)] public virtual DateTime? StartDate { get; set; } public virtual string Description { get; set; } [StringLength(100)] public virtual string Slug { get; set; } public virtual int MaximumCapacity { get; set; } [DataType(DataType.Currency)] public virtual int TicketPrice { get; set; } public virtual int LocationId { get; set; } public virtual Location Location { get; set; } public virtual Collection<Ticket> Tickets { get; set; } public virtual Collection<AttendeeInformationField> CaptureAttendeeInformationFields { get; set; } public virtual int CustomerId { get; set; } [Required] public virtual CUSTOMER Customer { get; set; } } 

The Save () method is the MagnetEventRepository method, which is taken from the above class.

Another update I successfully removed the error by changing the MagnetEventId in AttendeeInformationField to null. When examining the database, I can see for sure what is wrong.

Let's say I have one AttendeeInformationField with the value "E-mail". When I edit MagnetEvent, the AttendeeInformationField updates MagnetEventId to zero, and then adds a new message with the correct MagnetEventId and Value.

I would really like the posts in the AttendeeInformationField to be updated instead.

0
source share
4 answers

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

The solution is to override the default behavior, 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 worked only when updating existing collection elements. To solve this problem, 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. At the moment, I have not tried to delete.

However, I hope there is an easier way to do this. It seems that a lot of coding does a very basic job.

0
source

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

It may be that UpdateModel will change some information about the associated objects and that it is not so good. Not sure about this, although I don't see all the code.

I prefer non-uder UptadeModel and instead use the input model or your MVC model as input parameter and manually map the chages to the loaded source object.

The problem with the anthograph is that I do not see eventRepository.Save ();

really do saveshages? Does it have? Can I use some context code in another Save method?

0
source

As an exception, they say that these seams, like collections associated with it or other related objects, cannot find the actual value of the identifier.

Do you want to load related objects? how is the client?

0
source

It should be noted that you should not have [Required] for the Client, since it was deduced from the fact that your FK is not NULL. Required should only be used for a navigation property if you do not have an FK in the model.

To try to diagnose the problem, whether you can load the object and view it in the debugger, you should expect both locationId and CustomerId to have non-zero values.

0
source

Source: https://habr.com/ru/post/1341511/


All Articles