我的数据库中有 3 个表
CmsMasterPages
CmsMasterPagesAdvSlots (Pure Juction Table)
CmsAdvSlots
这是我的 EDM 的图片:
我需要找出与 CmsMasterPage 连接的所有 CmsAdvSlot 对象(它在我发布的代码中工作,亲爱的), 并从数据库中删除结果 (CmsAdvSlot)。
我的问题是当我找到主题时,我无法删除此对象。
Error: The object cannot be deleted because it was not found in the ObjectStateManager.
int findMasterPageId = Convert.ToInt32(uxMasterPagesListSelector.SelectedValue);
CmsMasterPage myMasterPage = context.CmsMasterPages.FirstOrDefault(x => x.MasterPageId == findMasterPageId);
var resultAdvSlots = myMasterPage.CmsAdvSlots;
// It is working until here
foreach (var toDeleteAdv in resultAdvSlots)
{
context.DeleteObject(myMasterPage.CmsAdvSlots.Any()); // ERORR HERE!!
context.SaveChanges();
}
知道如何解决吗?谢谢你的时间! :-)
最佳答案
试试这个:
while (myMasterPage.CmsAdvSlots.Count > 0)
{
var slot = myMasterPage.CmsAdvSlots.First();
myMasterPage.CmsAdvSlots.Remove(slot); // This is required only in some scenarios - it depends on type of entities you are using.
context.DeleteObject(slot);
}
context.SaveChanges();
关于c# - Entity Framework 4 - 删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5039476/