locked
The relationship could not be changed because one or more of the foreign-key properties is non-nullable RRS feed

  • Question

  • User-1707248210 posted


    I am new to ASP.NET MVC, and I have this problem:

    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.

    Mi method in Juego.cs:

     public void UpdateJuegos(JuegoDTO juegosDTO)
            {
                using (var dbContext = new TrailersVideoJuegosEntities())
                {
                    var juego = dbContext.Juego.FirstOrDefault(c => c.Id == juegosDTO.Id);
                    if (juego != null)
                    {
                        Mapper.Map(juegosDTO, juego);
                        Mapper.Map(juegosDTO.ListaImagenes, juego.Imagen);
                        
                        
                        if(juegosDTO.ListaImagenes != null)
                        {
                            foreach(var imagen in juegosDTO.ListaImagenes)
                            {
                                if(imagen.ImagenDeleted)
                                {
                                    var deletedImg = juego.Imagen.FirstOrDefault(r => r.Id == imagen.Id);
                                    if(deletedImg != null)
                                    {
                                        juego.Imagen.Remove(deletedImg);
                                        
                                    }
                                }
                            }                        
                        }                    
                        dbContext.SaveChanges();
                    }                
                }
            }

    Help me please!!!

    Thursday, December 15, 2016 8:48 AM

All replies