获取指定的实体不包含在此EntitySet中.错误 [英] Getting The specified entity is not contained in this EntitySet. error

查看:86
本文介绍了获取指定的实体不包含在此EntitySet中.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用silverlight mvvm light框架.当我尝试删除实体时,我得到了指定的实体不包含在此EntitySet中.错误,但在我的数据库中该实体已经存在.
在这段代码中,我遇到了错误.

这是我执行删除操作的代码

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

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);
       };
   }

推荐答案

这是因为您正在创建表单节对象的新实例.数据上下文不知道此对象,因此无法对其进行跟踪.使用上下文功能之前,需要将对象附加到上下文.
例如;

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;

context.Attach(fs);



话虽这么说,为什么您不更改代码以按ID删除,而不是创建form节的实例并为其分配值.您的方法似乎没必要.

例如;



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;

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);
       };
   }


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);
           };


这篇关于获取指定的实体不包含在此EntitySet中.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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