从Entity Framework导航属性中删除所有记录 [英] Removing all records from navigation properties in Entity Framework

查看:322
本文介绍了从Entity Framework导航属性中删除所有记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序和学生表之间有1:N关系,EF转换为导航属性。现在我想删除这个导航学生的所有记录。
我开始这样:

  foreach(学生在program.Students)
程序。学生。删除(学生);

但是我对此有一点怀疑。



比我这样尝试:

  while(program.Students.Count> 0)
程序.Students.Remove(program.Students.ToList()[0]);

但这似乎也很奇怪。



有没有一些更简单的方法来做,如果不是最好的方法?

解决方案

我真的不知道是否会不会工作,但我不能帮助,我很好奇。
program.Students.Clear()工作?
或者也许重置它,重新初始化?
我希望它帮助你...


I have 1:N relationship between Program and Student tables which EF converted into navigation property. Now I want to delete all those records in this navigation students. I started like this:

foreach(Student student in program.Students)
program.Students.Remove(student);

But I am a little bit skeptical about this.

Than I tried this way:

while (program.Students.Count > 0)
    program.Students.Remove(program.Students.ToList()[0]);

But this seems weird too.

Is there some simpler way to do this or if not which way is the best?

解决方案

I really don't know if it's gonna work or not, but I can not help it, I'm curious. Is program.Students.Clear() working? Or maybe reset it, reinitializing it? I hope it helped you...

这篇关于从Entity Framework导航属性中删除所有记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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