404NotFound
404NotFound

Reputation: 635

Delete Action in asp.net MVC using DB context

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; }
}

Upvotes: 0

Views: 3239

Answers (2)

user3383479
user3383479

Reputation:

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.

Upvotes: 1

tschmit007
tschmit007

Reputation: 7790

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.

Upvotes: 0

Related Questions