Note that there are some explanatory texts on larger screens.

plurals
  1. POForeign key constraint, EF with collection of childobjects
    primarykey
    data
    text
    <p>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. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted."</p> <p>From what I understand from <a href="https://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign-key-pro">The relationship could not be changed because one or more of the foreign-key properties is non-nullable</a> the problem might be with how Entity Framework handles my virtual ICollection</p> <p>However I'm not really sure how to implement the solution when using scaffolded repository pattern. Do I have to edit the Save()-method ParentObjectRepository-class?</p> <p>Actually I really think that there must be some way to make EF understand this. I can't see how the EF-team was thinking "Probably noone is using a collection of objects with a foreign key constraint, lets not support that".</p> <p><strong>Update</strong> Added code</p> <pre><code>[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(); } </code></pre> <p>More code:</p> <pre><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&lt;Ticket&gt; Tickets { get; set; } public virtual Collection&lt;AttendeeInformationField&gt; CaptureAttendeeInformationFields { get; set; } public virtual int CustomerId { get; set; } [Required] public virtual CUSTOMER Customer { get; set; } } </code></pre> <p>The Save()-method is from MagnetEventRepository, which is scaffolded from the above class.</p> <p><strong>Another update</strong> I successfully removed the error by changing MagnetEventId in AttendeeInformationField to nullable int. When examining the database I can see exactly what's wrong.</p> <p>Let's say I have a single AttendeeInformationField with the value "E-mail". When I edit my MagnetEvent, the AttendeeInformationField updates the MagnetEventId to null and then adds a new post with the correct MagnetEventId and Value.</p> <p>I'd very much prefer if the posts in AttendeeInformationField were updated instead.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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