0

I have a problem with my delete action, I'm getting this exception :

A first chance exception of type 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll

Additional information: Entities in 'Model1Container.PublicationSet' participate in the 'PublicationQuartier' relationship. 0 related 'Quartier' were found. 1 'Quartier' is expected.

Here my Delete Action :

        [HttpPost]
    public ActionResult DeleteOffreLocation(int id, OffreLocation offreLocation)
    {
        try
        {
            db.Entry(offreLocation).State = System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();

            return RedirectToAction("ListOffreLocation");
        }
        catch
        {
            return View();
        }
    }

"OffreLocation" Model :

public partial class OffreLocation : Publication
{
    public OffreLocation()
    {
        this.Locataire = new HashSet<Locataire>();
        this.DemandeLocation = new HashSet<DemandeLocation>();
        this.DemandeLocation1 = new HashSet<DemandeLocation>();
        this.DemandeLocation2 = new HashSet<DemandeLocation>();
        this.DemandeLocation3 = new HashSet<DemandeLocation>();
    }

    public string OffreLocation_TypeLog { get; set; }
    public string OffreLocation_Sante { get; set; }
    public double OffreLocation_Loyer { get; set; }
    public System.DateTime OffreLocation_DateDisponibilite { get; set; }
    public double OffreLocation_Superficie { get; set; }
    public short OffreLocation_NbreChambre { get; set; }
    public short OffreLocation_NbrePieces { get; set; }

    public virtual ICollection<Locataire> Locataire { get; set; }
    public virtual Proprietaire Proprietaire { get; set; }
    public virtual ICollection<DemandeLocation> DemandeLocation { get; set; }
    public virtual ICollection<DemandeLocation> DemandeLocation1 { get; set; }
    public virtual ICollection<DemandeLocation> DemandeLocation2 { get; set; }
    public virtual ICollection<DemandeLocation> DemandeLocation3 { get; set; }
}

"Publication" Model:

 public partial class Publication
{
    public Publication()
    {
        this.Photo = new HashSet<Photo>();
    }

    public int Publication_ID { get; set; }
    public string Publication_Statut { get; set; }
    public bool Publication_Meublee { get; set; }
    public string Publication_Descriptif { get; set; }
    public bool Publication_ContactParAgence { get; set; }
    public double Publication_Maps_Latitude { get; set; }
    public double Publication_Maps_Longitude { get; set; }

    public virtual Quartier Quartier { get; set; }
    public virtual ICollection<Photo> Photo { get; set; }
}

and finaly here my DB Container:

 public partial class Model1Container : DbContext
{
    public Model1Container()
        : base("name=Model1Container")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<Utilisateur> UtilisateurSet { get; set; }
    public DbSet<Abonnement> AbonnementSet { get; set; }
    public DbSet<AbonnementHistorique> AbonnementHistoriqueSet { get; set; }
    public DbSet<ColocataireIdeal> ColocataireIdealSet { get; set; }
    public DbSet<Publication> PublicationSet { get; set; }
    public DbSet<Quartier> QuartierSet { get; set; }
    public DbSet<Ville> VilleSet { get; set; }
    public DbSet<RegionProvince> RegionProvinceSet { get; set; }
    public DbSet<Photo> PhotoSet { get; set; }
    public DbSet<MessageLocation> MessageLocationSet { get; set; }
    public DbSet<MessageColocation> MessageColocationSet { get; set; }

    public System.Data.Entity.DbSet<COM.MENNILIK.Models.Locataire> Locataires { get; set; }

    public System.Data.Entity.DbSet<COM.MENNILIK.Models.OffreLocation> OffreLocations { get; set; }
}
0

2 Answers 2

1

Actually you need to remove the object passed to the view. You can either use id or your model OffreLocation. I will give you an example with id.

 [HttpPost]
public ActionResult DeleteOffreLocation(int id, OffreLocation offreLocation)
{
    try
    {
        var offreLocationGetById =db.OffreLocations.Find(id);
        if(offreLocationGetById!=null)
        db.OffreLocations.Remove(offreLocationGetById);

        db.SaveChanges();

        return RedirectToAction("ListOffreLocation");
    }
    catch
    {
        return View();
    }
}

I will suggest also you comment your OnModelCreating as you are not using FluentApi code and beside you throw an Exception. It is a suggestion.

Sign up to request clarification or add additional context in comments.

1 Comment

I recommend Inanikian's answer. The reason being is when the program enters "DeleteOffreLocation" action, the mvc model binder returns a offreLocation object but entityframework cache doesn't contain this object it could not locate it therefore it reports error. To make it work, you need to FindById to get the object first and mark its status as deleted.
0

it seems to be a model validation error. I'm quite sure that Quartier is required and in the offreLocation from your Action, Quartier should be null.

so you have to :

  • populate Quartier, or
  • disable validation.

1 Comment

In fact, i've already populated my "Quartier" Table, but i still having this exception !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.