删除许多使用实体框架许多关系 [英] Delete Many to Many relationship using Entity Framework

查看:110
本文介绍了删除许多使用实体框架许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表学生(studID,全名,性别......),登记(studID,courseID,日期),场(courseID,课程名,...)。我用下面的代码删除登记表studID 001那里有大约三课程的学生签署的所有记录。然而,这只是删除一条记录。

I've three tables Student (studID, fullName, gender...), Enroll (studID, courseID, date) and Course (courseID,courseName, ...). I used the code below to delete all records from Enroll table with studID 001 where there are about three courses the student signed for. However, it only deletes one record.

using(var context = new DBEntities())
{    
var _stud = (from s in context.Students where s.studID == "001" select s).FirstOrDefault();
                var _course = _stud.Courses.FirstOrDefault();
                _course.Students.Remove(_stud);
context.SaveChanges();
}



我怎么会错过吗?

What do I miss here?

推荐答案

感谢你们协助。这是我如何解决它:使用(VAR上下文=新DBEntities())
{

Thank you guys for assisting. Here is how I solved it:

        using (var context = new DBEntities())
        {
            var _stud = (from s in context.Students where s.studID == "001" select s).FirstOrDefault<Student>();

            foreach (Course c in _stud.Courses.ToList())
            {
                _stud.Courses.Remove(c);
            }
            context.SaveChanges();
        }

这篇关于删除许多使用实体框架许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆