You must load the child questions into the context. If you delete the parent EF should set the reference CreatedFrom
for all attached children to null
and save this as an UPDATE to the database:
using (var context = new MyContext())
{
var parentQuestion = context.Questions
.SingleOrDefault(q => q.Id == someId);
if (parentQuestion != null)
{
context.Questions
.Where(q => q.CreatedFrom.Id == someId)
.Load();
context.Questions.Remove(parentQuestion);
context.SaveChanges();
}
}
If you have a ChildQuestions
collection property in Question
you can simplify this by adding Include(q => q.ChildQuestions)
to the query for the parentQuestion
and remove the query that loads the children explicitly.