简体   繁体   English

没有实体框架的MVC3多对多

[英]MVC3 Many to many without Entity Framework

I'm creating a MVC3 sample project instead of Entity Framework. 我正在创建一个MVC3示例项目,而不是实体框架。 I created an API (basically a dll) which contains Teacher School and Qualification Classes and DBOperation class to manage the CRUD operations. 我创建了一个API(主要是dll),其中包含教师学校和资格课程以及DBOperation课程来管理CRUD操作。

Teacher and Qualification have Many to many Relationship (One particular teacher can have more than one education qualifications). 教师和资格具有多对多的关系(一位特定的老师可以具有一个以上的教育资格)。

I managed to complete the code for One to many (teacher-Qualification) and successfully created the Controllers and views. 我设法完成了一对多(教师资格)的代码,并成功创建了控制器和视图。

But I am stuck when it comes to Many to many. 但是当涉及到多对多时,我陷入了困境。

Help needed in this. 在此需要帮助。 Thank you. 谢谢。

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();
    }

} }

Any many to many relationship will need an intermediate table. 任何多对多关系都需要一个中间表。 something like this: 像这样的东西:

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

This would be the model representation. 这将是模型表示。 You could swap ID out with model if you would like. 如果愿意,可以将ID与模型交换出去。 You will need a database table that resembles this model as well. 您还将需要一个与此模型相似的数据库表。

When you are adding your teacher you will also need to insert into this new table for each qualification that a teach has. 当您添加老师时,您还需要针对该教师具有的每个资格将其插入此新表中。 One row for each qualification, including the TeacherId and the specific QualificationId . 每项资格要求一行,包括TeacherId和特定的QualificationId

When you go to retrieve the teacher you will also need to query this intermediate table by TeacherId to get the list of QualificationId s that the given teacher has. 当您检索老师时,您还需要通过TeacherId查询此中间表,以获取给定老师具有的QualificationId的列表。 At this point you can then query the Qualification table to get the actual qualifications for the teacher. 此时,您可以查询“资格”表以获取教师的实际资格。 IF you want you could also do this in a single query using Joins. 如果您希望也可以使用联接在单个查询中执行此操作。

Hopefully this helps. 希望这会有所帮助。

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

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