2014-07-17 132 views
1

我有點新手用asp.net MVC環境刪除行ASP.NET MVC ADO.NET實體模型

我試圖從3表/實體刪除ROW(因爲我是用ado.net實體數據模型自動生成數據庫)時只執行我的刪除功能從1個表中刪除的行問題是..

PS:我也已經建立3個表

這裏的關係是我的控制器:

[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"); 
    } 

這是我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; } 
    } 
} 

這是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; } 

    } 
} 

最後我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; } 
    } 
} 

只是你知道,在生成模型的代碼自動,我使用的ado.net實體模型和唯一的表/實體刪除只是ms_student表(對不起,我有點混淆與命名:模型或實體或表)

上ms_person的ID是自動增量PK 上ms_user其實FK也ID的ms_person 的PK和(愚蠢我爲不同的命名)user_id_student其實FK也ms_person

感謝的PK你很

回答

4

我想我只是發現了一些解決方案, 我改變我刪除控制器與此:

[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"); 
    } 

這一切從3表中的記錄被刪除,但我不知道這是最好的解決辦法,因爲你可以看到,我只是修改了include()語句,我真的不知道這個方法可以在其他控制器以及使用...

非常感謝你,如果下跌免費有人可以爲我的問題提供更好的解決方案..:D

+0

票了:)不過你也可以使用無單在哪裏,就像這樣:.INCLUDE(I => i.ms_user)。單(I => i.ID == ID); – DaniKR

1

你也想去掉ms_person和ms_user?

執行此操作。

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(); 
+0

謝謝您的回覆Yuliam,不幸在ms_person記錄不會被刪除,但ms_student和ms_user記錄被刪除.. 我想這是因爲實體依賴,因爲ms_user和ms_student居然不有什麼PK IF ms_person不存在的,所以我猜,如果你想從3臺完全刪除所有記錄,U必須首先刪除ms_person ... 請注意,我說的不是上刪除級聯或更新級聯。 – user3848402

0
[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"); 
} 
+0

請縮進你的代碼,它根本不可讀。 –