【问题标题】:MVC3 Many to many without Entity FrameworkMVC3 多对多没有实体框架
【发布时间】:2014-04-11 05:46:57
【问题描述】:

我正在创建一个 MVC3 示例项目而不是实体框架。我创建了一个 API(基本上是一个 dll),其中包含教师学校和资格类以及 DBOperation 类来管理 CRUD 操作。

Teacher 和 Qualification 具有多对多关系(一位特定的教师可以拥有多个教育资格)。

我设法完成了一对多(教师资格)的代码并成功创建了控制器和视图。

但是当涉及到多对多时我被卡住了。

这方面需要帮助。谢谢。

public class School
{
    public int SchoolID { get; set; }
    public string Name { get; set; }
    public string Telephone { get; set; }
    public string Address { get; set; }
}


public class Teacher
{
    public int TeacherID { get; set; }
    public string Name { get; set; }
    public string NIC { get; set; }
    public string Address { get; set; }
    public string Telephone { get; set; }
    public int School { get; set; }
    //public List<Qualification> Qualification { get; set; } if teacher got many Qualifications
    public int Qualification { get; set; }
}



public class Qualification
{
    public int QualificationID { get; set; }

    public string Type { get; set; }

}



public class DBOperations
{
    public List<Teacher> GetAllTeachers()
    {
        List<Teacher> t = new List<Teacher>();
        string connectionString = Connection.ConnectionString;
        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT * FROM Teachers";
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read()) 
        {
            Teacher teacher = new Teacher();
            teacher.TeacherID = dr.GetInt32(0);
            teacher.Name = dr.GetString(1);
            teacher.NIC = dr.GetString(3);
            teacher.Address = dr.GetString(4);
            teacher.Telephone = dr.GetString(4);
            teacher.School = dr.GetInt32(5);
            teacher.Qualification = dr.GetInt32(6);
            t.Add(teacher);


        }
        return t;

    }

    public void AddNewTeacher(Teacher teacher)
    {
        string Name = teacher.Name;
        string NIC = teacher.NIC;
        string Address = teacher.Address;
        string Telephone = teacher.Telephone;
        int School = teacher.School;
        int Qualification = teacher.Qualification;

        string connectionString = Connection.ConnectionString;
        SqlConnection conn = new SqlConnection(connectionString);
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "INSERT INTO Teachers VALUES('" + Name + "','" + NIC + "','" + Address + "','" + Telephone + "','" + School + "','" + Qualification + "')";
        cmd.ExecuteNonQuery();
        conn.Close();
    }

}

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3


    【解决方案1】:

    任何多对多关系都需要一个中间表。像这样:

    public class TeachersQualifications
    {
        public int QualificationId { get; set; }
        public int TeacherId { get; set; }
    }
    

    这将是模型表示。如果您愿意,可以将 ID 与模型交换。您还需要一个类似于此模型的数据库表。

    添加教师时,您还需要在此新表中插入教师所拥有的每项资格。每个资格占一行,包括TeacherId 和特定的QualificationId

    当您去检索教师时,您还需要通过TeacherId 查询这个中间表以获取给定教师拥有的QualificationIds 列表。此时,您可以查询 Qualification 表以获取教师的实际资格。如果您愿意,也可以使用 Joins 在单个查询中执行此操作。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多