我的删除操作有问题,我遇到了这个例外:
类型的第一次机会异常 发生'System.Data.Entity.Infrastructure.DbUpdateException' EntityFramework.dll
其他信息:'Model1Container.PublicationSet'中的实体 参加'PublicationQuartier'的关系。 0相关 发现'Quartier'。预计1'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; }
}
最后我的数据库容器:
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 :(得分:1)
实际上你需要删除传递给视图的对象。您可以使用id
或您的模型OffreLocation
。我将以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();
}
}
我建议您在OnModelCreating
使用FluentApi
代码时对Exception
进行评论,然后在旁边抛出{{1}}。这是一个建议。
答案 1 :(得分:0)
它似乎是模型验证错误。我非常确定Quartier
是必需的,在你的Action的offreLocation中,Quartier应为null。
所以你必须: