Delete row ASP.NET MVC ADO.NET entity Model

13,594

Solution 1

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...

thank you very much, fell free if somebody can put a better solution for my problem.. :D

Solution 2

You want to also remove the ms_person and 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();
Share:
13,594
user3848402
Author by

user3848402

Updated on June 04, 2022

Comments

  • user3848402
    user3848402 almost 2 years

    I'm kinda newbie with asp.net MVC environment

    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..

    PS: i've also already create the relationship between 3 table

    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:

    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:

    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:

    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)

    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

    thank you very much

  • user3848402
    user3848402 almost 10 years
    Thank you for your reply Yuliam, unfortunately record on ms_person is not deleted, but the record on ms_student and ms_user is deleted.. i think it because the entity dependency, because ms_user and ms_student actually don't have any PK IF ms_person don't exist, so i'm guessing that if you want to completely delete all the record from 3 table, u must make delete the ms_person first... Note that i'm not talking about on delete cascade or on update cascade.
  • DaniKR
    DaniKR over 9 years
    vote up :) but you can also use Single without Where, like this: .Include(i => i.ms_user).Single(i => i.ID == id);