在数据库中插入多对多数据

时间:2016-01-05 16:54:17

标签: asp.net-mvc entity-framework linq many-to-many

我试图将数据添加到“多对多”关系中。

这是我的控制器

[HttpPost]
public ActionResult AddSkillsPost()
{
    int Teacher_ID = Convert.ToInt32(Session["Teacher_ID"]);
    var SkillsArray = Request.Form["chk_group[]"];
    if(SkillsArray != null)
    {
        foreach(var skill in SkillsArray)
        {
            int Skill_ID = Convert.ToInt32(skill);

            var SkillToBeModified = (from s in db.Skills
                                     where s.ID == Skill_ID
                                     select new Skill { ID = s.ID, SkillName = s.SkillName, Teachers = s.Teachers });

            var Teacher = (from t in db.Teachers
                           where t.ID == Teacher_ID
                           select new Teacher { ID = t.ID, FirstName = t.FirstName, LastName = t.LastName,
                                                Email = t.Email, Campus = t.Campus, Skills = t.Skills });

            SkillToBeModified.Teachers.Add(Teacher);
            db.Entry(SkillToBeModified).State = EntityState.Modified;

            db.SaveChanges();
        }
    }
    return RedirectToAction("MyProfile");
}

所以我试图将老师添加到某项技能中。但是,我遇到了这条线的问题

SkillToBeModified.Teachers.Add(Teacher);

我收到错误

  

' IQueryable的'不包含'教师'的定义和   没有推广方法'教师'接受第一个类型的参数   ' IQueryable的'可以找到(你是否错过了使用指令   或汇编参考?)

我假设这是一个语法错误(查询的内容?),但我无法弄清楚写它的正确方法。

这是我的模型 for Skills

[Table("Skills")]
public class Skill
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required]
    public string SkillName { get; set; }

    public virtual List<Teacher> Teachers { get; set; }
    public Skill()
    {
        this.Teachers = new List<Teacher>();
    }
}

以及数据库的样子

enter image description here

期待任何回复

编辑:表单

<form name="addSkillsForm" action="AddSkillsPost" method="post">
    @foreach (var skill in Model.Skills)
    {
        <input type="checkbox" name="chk_group[]" value="@skill.ID" />@skill.SkillName< br />
    }
    <input type="submit" value="Update Skills" />
</form>

我使用的是一个复选框表单,用户可以在其中检查多个项目,也许我没有在我的POST中直接处理它们?

1 个答案:

答案 0 :(得分:2)

如果您的数据库看起来像图片,您应该可以这样做。

[HttpPost]
public ActionResult AddSkillsPost()
{
    int teacher_ID = Convert.ToInt32(Session["Teacher_ID"]);
    var SkillsArray = Request.Form["chk_group[]"];
    if (SkillsArray != null)
    {
        foreach (var skill in SkillsArray.Split(','))
        {
            int skill_ID = Convert.ToInt32(skill);
            db.TeacherSkills.Add(new TeacherSkill() { Teacher_ID = teacher_ID, Skill_ID = skill_ID });
            db.SaveChanges();
        }
    }
    return RedirectToAction("MyProfile");
}

但是,如果您要将教师添加到技能中,那么您就可以这样做。

[HttpPost]
public ActionResult AddSkillsPost()
{
    var teacher_ID = Convert.ToInt32(Session["Teacher_ID"]);

    var SkillsArray = Request.Form["chk_group[]"];
    if (SkillsArray != null)
    {
        var teacher = db.Teachers.Find(teacher_ID);
        foreach (var skill in SkillsArray.Split(','))
        {
            var skill_ID = Convert.ToInt32(skill);
            var skillToBeModified = db.Skills.Find(skill_ID);
            skillToBeModified.Teachers.Add(teacher);
            db.Entry(skillToBeModified).State = EntityState.Modified;
            db.SaveChanges();
        }
    }
    return RedirectToAction("MyProfile");
}