我有我的删除操作,我得到这个异常的问题:删除操作使用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