Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
In this code I am getting error.

This is my code where I am doing delete operation

C#
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);
       };
   }
Posted

This is because you are creating a new instance of your form section object. The data context is unaware of this object and cannot track it. You need to Attach the object to the context before you use the context features.
eg;

C#
context.Attach(fs);


That being said why don't you rather alter your code to delete by Id rather than create an instance of form section and assign values to it. Your approach just seems unnecessary.

Eg;

C#
public void DeleteSectionQuestion(Form currentForm,CustomSectionTree selectedSectionQuestion, DeleteDelegate callback)
   {
       foreach (Question q in selectedSectionQuestion.Questions)
       {
           context.FormSections.Remove(context.FormSections.FirstOrDefault(x => x.FormID == currentForm.FormID));
       }

       SubmitOperation so = context.SubmitChanges();
       so.Completed += (s, args) =>
       {
           if (so.HasError)
           {
               so.MarkErrorAsHandled();
               callback.Invoke(false, so.Error);
           }
           else
               callback.Invoke(true, null);
       };
   }
 
Share this answer
 
C#
FormSection formSection= context.FormSections.Where(c => c.FormID == currentForm.FormID && c.SectionID == selectedSectionQuestion.SectionID && selectedSectionQuestion.Questions.Any(q=>q.QuestionID==c.QuestionID)).FirstOrDefault();
           context.FormSections.Remove(formSection);
           SubmitOperation so = context.SubmitChanges();
           so.Completed += (ss, argss) =>
           {
               if (so.HasError)
               {
                   so.MarkErrorAsHandled();
                   callback.Invoke(false, so.Error);
               }
               else
                   callback.Invoke(true, null);
           };
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900