我有两张桌子升降机
public class Lift()
{
[Key]
public int LiftID { get; set; }
public string StartPoint { get; set; }
public virtual List<RequestLift> requestLifts { get; set; }
}
然后是RequestLift表
public class RequestLift()
{
[Key]
public int RequestLiftID { get; set; }
public int DriverID { get; set; }
}
DriverID是LiftID的外键。我希望能够删除电梯,因此请求升降机。在我的控制器中我有
[HttpGet]
public ActionResult Delete(int id)
{
//if (id == null)
//{
// return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
//}
Lift lift = db.Lifts.Find(id);
db.Lifts.Remove(lift);
db.SaveChanges();
return RedirectToAction("Index");
}
然后在我的身份模型中
public DbSet<Lift> Lifts { get; set; }
public DbSet<WeeklyLift> weeklyLifts { get; set; }
public DbSet<RequestLift> requestLifts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Lift>()
.HasOptional(l => l.requestLifts)
.WithMany()
.WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder)
}
但是,当我尝试删除两个表中包含键的内容时,会发生未处理的异常错误。