I am using silverlight mvvm light framework. when I was trying to delete entity I got The specified entity is not contained in this EntitySet. error but in my Database that entity is already there.
context.FormSection.Remove(formSection);
In this code I am getting error.
This is my code where I am doing delete operation
public void DeleteSectionQuestion(Form currentForm,CustomSectionTree selectedSectionQuestion, DeleteDelegate callback)
{
FormSection fs = new FormSection();
foreach (Question q in selectedSectionQuestion.Questions)
{
fs.FormID = currentForm.FormID;
fs.SectionID = selectedSectionQuestion.SectionID;
fs.QuestionID = q.QuestionID;
context.FormSections.Remove(fs);
}
SubmitOperation so = context.SubmitChanges();
so.Completed += (s, args) =>
{
if (so.HasError)
{
so.MarkErrorAsHandled();
callback.Invoke(false, so.Error);
}
else
callback.Invoke(true, null);
};
}
Look at the following piece of your code:
You are creating a new instance of
FormSection, and then try to delete it multiple times.Entities/EntitySetdon’t work that way: the context tracks which entities you retrieved from the database. It means that if you try to remove an entity that is not an instance of an entity that came from the database, it is unknown for the context.You should have a list of queried
FormSectionsin your viewmodel, you should delete instances ofFormSectionthat come from this list so they are known by the context.