何か奇妙なことが起こります。UserGroups という名前のテーブルと 1 対多の関係を持つ Group という名前のテーブルがあります。
Entity Framework .Remove メソッドを使用するようになりました。ユーザーを含むグループを削除することはできますが、データベースで直接同様の操作を実行しようとすると、例外が発生します (グループに子レコードがあること!!!) 何が起こっているのかわかりません!!!.
Action メソッドは次のようになります:-
[HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                var v = groupRepository.Find(id).Name;
                groupRepository.Delete(id);
                groupRepository.Save();
                return Json(new { IsSuccess = "True", id = id, description = v }, JsonRequestBehavior.AllowGet);
              //  return RedirectToAction("Index");
            }
            catch (NullReferenceException)
            {
                //ModelState.AddModelError(string.Empty, " The record might have been already deleted.Please refresh the page.");
                return Json(new { IsSuccess = "False" }, JsonRequestBehavior.AllowGet);
            }
return RedirectToAction("Delete", new { id = id});
        }
リポジトリのメソッドは次のとおりです。
public void Delete(int id)
        {
            var group = context.Groups.Find(id);
            context.Groups.Remove(group);
        }
public Group Find(int id)
        {
            return context.Groups.Where(c => c.GroupID == id).Include(a => a.UserGroups)
                .Include(a2 => a2.SecurityRoles).SingleOrDefault();
        } 
残念ながら、Find メソッドで Group とその 2 つのナビゲーション プロパティを取得しているため、.Delete は最初にこれらのナビゲーションを削除し、次に Group オブジェクトを削除することを意味します !!!!
編集
2 つのメソッドを定義しました。検索とすべてを検索:-
public Group Find(int id)
        {
            return context.Groups.Find(id) ;
        }
        public Group FindAll(int id)
        {
            return context.Groups.Where(c => c.GroupID == id).Include(a => a.UserGroups)
                .Include(a2 => a2.SecurityRoles).SingleOrDefault();
        }
他の提案はありますか??