我有点新手用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你很
票了:)不过你也可以使用无单在哪里,就像这样:.INCLUDE(I => i.ms_user)。单(I => i.ID == ID); – DaniKR