简体   繁体   English

删除行ASP.NET MVC ADO.NET实体模型

[英]Delete row ASP.NET MVC ADO.NET entity Model

I'm kinda newbie with asp.net MVC environment 我是asp.net MVC环境的新手

i'm trying to delete ROW from 3 table/entity(cause i'm using ado.net entity data model to generate the database automatically) the problem is when my delete function is executed only ROW from 1 table is deleted.. 我正在尝试从3个表/实体中删除ROW(因为我正在使用ado.net实体数据模型来自动生成数据库),问题是当我执行删除功能时,仅从1个表中删除了ROW。

PS: i've also already create the relationship between 3 table PS:我也已经创建了3个表之间的关系

here is my controller: 这是我的控制器:

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        // i've edited this code, i think the problem lies in this code bellow
        // start edited code
        ms_student ms_student = db.ms_student
             .Include(i => i.ms_person) 
             .Include(i => i.ms_person.ms_user)
             .Where(i => i.user_id_student == id)
             .Single(); 
        // end edited code

        db.ms_student.Remove(ms_student);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

this is my ms_user model: 这是我的ms_user模型:

namespace test.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ms_user
    {
        public int ID { get; set; }
        public string password { get; set; }
        public string salt { get; set; }
        public Nullable<int> administrative_type_id { get; set; }
        public string email_login { get; set; }

        public virtual ms_person ms_person { get; set; }
    }
}

this is ms_person model: 这是ms_person模型:

namespace test.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ms_person
    {  
        public int ID { get; set; }
        public Nullable<int> family_id { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string email { get; set; }
        public string address { get; set; }
        public string phone_address { get; set; }
        public string mobile_phone_number { get; set; }
        public string gender { get; set; }
        public Nullable<System.DateTime> date_of_bith { get; set; }
        public Nullable<System.DateTime> date_start { get; set; }
        public Nullable<System.DateTime> date_end { get; set; }
        public string status { get; set; }
        public string identification_ID { get; set; }
        public string passport { get; set; }
        public Nullable<int> user_type_id { get; set; }
        public string file_image { get; set; }

        public virtual ms_student ms_student { get; set; }
        public virtual ms_user ms_user { get; set; }

    }
}

Lastly my ms_person model: 最后,我的ms_person模型:

namespace test.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ms_student
    {
        public int user_id_student { get; set; }
        public int student_code { get; set; }
        public int course_id { get; set; }
        public string degree { get; set; }
        public Nullable<int> current_semester { get; set; }
        public string cuti_session { get; set; }

        public virtual ms_person ms_person { get; set; }
    }
}

just you know that the model code is auto generated, i'm using ado.net entity model and the only table/entity that deleted are only the ms_student table(sorry, i'm kinda confused with naming: model or entity or table) 只有您知道模型代码是自动生成的,我使用的是ado.net实体模型,并且删除的唯一表/实体只是ms_student表(对不起,我有点与命名混淆:模型,实体或表)

the ID on ms_person are auto increment PK the ID on ms_user actually the FK also the PK of ms_person and (foolishly of me for the different naming) user_id_student actually the FK also the PK of ms_person ms_person上的ID是自动递增PK ms_user上的ID实际上是FK也是ms_person的PK,(对于不同的命名我是愚蠢的)user_id_student实际上FK也是ms_person的PK

thank you very much 非常感谢你

I think i just found some solution, i change my delete controller with this: 我想我刚刚找到了一些解决方案,我用以下方法更改了删除控制器:

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        // i change this piece of code, not really sure what it means though
        // very appreciate if somebody can describe it in more humanly or sql language
        ms_person ms_person = db.ms_person
             .Include(i => i.ms_student)
             .Include(i => i.ms_user)
             .Where(i => i.ID == id)
             .Single();

        db.ms_person.Remove(ms_person);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

with this all the record from 3 table is deleted, but i'm not sure this is the best solution, because as you can see i just modified the Include() statement, and i'm not really sure this method can be used on other controller as well... 这样就删除了3表中的所有记录,但是我不确定这是最好的解决方案,因为如您所见,我只是修改了Include()语句,而且我不确定该方法是否可以用于其他控制器...

thank you very much, fell free if somebody can put a better solution for my problem.. :D 非常感谢您,如果有人可以为我的问题提供更好的解决方案,请放心..:D

You want to also remove the ms_person and ms_user? 您还想删除ms_person和ms_user吗?

Do this. 做这个。

db.ms_student.Remove(ms_student);
if (ms_student.ms_person != null)
{
   db.ms_person.Remove(ms_student.ms_person);
   if (ms_student.ms_person.ms_user != null)
   {
      db.ms_user.Remove(ms_student.ms_person.ms_user);
   }
}
db.SaveChanges();
[HttpGet]
public ActionResult DeleteFacilitator( Int64 FacID)
{
  if(FacID == null)
  {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  }
  var Resul= db.NFE_Facilitator.Find(ID);
  if(Resul==null)
  {
      return HttpNotFound();
  }
      return View(Resul);
}
[HttpPost,ActionName("DeleteFacilitator")]
public ActionResult DeleteConfirmed( Int64 ID)
{
   var Resul= db.NFE_Facilitator.Find(ID);
   db.NFE_Facilitator.Remove(Resul);
   db.SaveChanges();
   return RedirectToAction("SearchFacilitator");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM