ASP.NET :- 使用单一视图从两个表中删除记录

ASP.NET :- Deleting records from two tables using single view

我想删除 2 table 秒的数据。我正在使用 entity framework.

这是 class .

[HttpPost]
        public ActionResult Delete(int employeeId)
        {
            int id = employeeId;
            MvcWebApp.DbModel.profile profile = dbContext.profiles.Find(employeeId);
            dbContext.profiles.Remove(profile);
            dbContext.SaveChanges();
            regi regi = dbContext.regis.Find(id);
            dbContext.regis.Remove(regi);
            dbContext.SaveChanges();
            return RedirectToAction("Index");
        }

其中 employeeId 是子项 table 的列名,Id 是父项 table 的列名。

MvcWebApp.DbModel.profile 这是 class.

的完全限定名称

我得到 profile 对象为空。

我的建议是,首先检查您是否有提供您 ID 的员工,这是最简单的,我想一定没有!正如你所说 returns null。 您是否将所有输入都设为 NULL?

其次,尝试在调试器模式下 运行 您的代码,在第一行放置一个断点以查看将什么 EmployeeId 发送到方法。检查它可能为零,所以你的 POST 方法有问题

需要在父对象中包含子对象 table。

 [HttpPost]
         public ActionResult Delete(int id)
         {
            var parent = dbContext.regis.Include(p => p.profiles).SingleOrDefault(p => p.id == id);
            foreach(var child in parent.profiles.ToList())
            {
                dbContext.profiles.Remove(child);
                dbContext.SaveChanges();
            }
             return RedirectToAction("Index");
         }