我有我的刪除操作,我得到這個異常的問題:刪除操作使用DB方面
類型的第一個機會異常「System.Data.Entity.Infrastructure .DbUpdateException'發生在 EntityFramework.dll
其他信息:'Model1Container.PublicationSet' 中的實體參與'PublicationQuartier'關係。 0相關 'Quartier'被發現。預計將有'Quartier'。
這裏我刪除操作:
[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」 型號:
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; }
}
「出版」 型號:
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; }
}
和finaly這裏我DB集裝箱:
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; }
}
我推薦Inanikian的回答。原因在於當程序進入「DeleteOffreLocation」動作時,mvc模型聯編程序返回offreLocation對象,但實體框架緩存不包含此對象,因此它無法找到它,因此它會報告錯誤。爲了使它工作,你需要FindById首先獲取對象並將其狀態標記爲已刪除。 – Larry