c# - 删除 C 时删除 AB 之间的链接
问题描述
这是我的数据模型:
考试:
public class Exam
{
public int Id { get; set; }
public DateTime StartDate { get; set; }
public int TimeLimit { get; set; }
public User User { get; set; }
public List<Question> Questions { get; set; }
public QuestionSuite Suite { get; set; }
}
问题套件
public class QuestionSuite
{
public int Id { get; set; }
public List<Question> Questions { get; set; }
public User Owner { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public int TimeLimit { get; set; }
}
问题
/* Some data*/
public virtual List<Exam> Exams { get; set; }
public virtual List<Result> Results { get; set; }
public virtual List<QuestionSuite> QuestionSuites { get; set; }
Question
代表一个简单的问题,它的答案和东西。Exam
代表当前正在进行的考试,有用户在做,一些问题,并且可能是从QuestionSuite
. 最后,QuestionSuite
是任何用户都可以创建和共享的自定义问题套件。
有这么多参考的原因是我需要Exam
记住套件以在考试结束时通知创建者,但它仍然需要记住问题,因为并非所有考试都是从套件创建的。
现在解决问题:考试结束后,我创建一个结果,如果考试有结果,通知套件创建者,然后Exam
使用以下代码删除对象:
public void DeleteExamByUserId(int userId)
{
//Context.Exams.RemoveRange(Context.Exams.Where<Exam>(exam => exam.User.Id == userId));
foreach(Exam e in Context.Exams)
{
if (e.User.Id == userId)
Context.Exams.Remove(e);
}
Context.SaveChanges();
}
EntityFramework
创建一个QuestionSuiteQuestions
表来链接问题和套件。当一个考试被删除时,QuestionSuiteQuestions
也被无端删除。
你知道为什么会这样吗?有没有办法在某处查看关系和删除背后的逻辑?
解决方案
推荐阅读
- c++ - 使用 valgrind 进行堆分配
- web-crawler - 根据 StormCrawler 中的优先级抓取 URL
- spring-boot - Fortify-scanner : 密码管理:配置文件中的密码
- php - 发送http post请求并直接获取post code
- angular - 向 API 发布请求不起作用 | 角 10.2
- html - 将 Bootstrap 列保留在视口中
- ios - 从 Xcode 运行时,Shell 脚本不会完全执行
- node.js - next.config.js 如何设置环境变量?
- model-view-controller - 限制要保存的 html 标签
- c# - 如何使用 C# 在旧版边缘浏览器的新窗口中启动 url