2012-03-13 16 views
1

我有以下寄存器的操作方法: -不能趕上DbUpdateException我repositiry提出在我的控制器方法

[AcceptVerbs(HttpVerbs.Post)] 
     public PartialViewResult Register(string id, int classid) { 

      try 
{ 
User user = r.FindUser(id); 
      Users_Classes uc = new Users_Classes(); 
      uc.AddedDate = DateTime.Now; 
      uc.ClassID = classid; 
      user.Users_Classes.Add(uc); 
      repository.Save(); 
      ViewBag.classid = classid; 
      return PartialView("_usersearch2", uc); 
} 
    catch (System.Data.UpdateException ex) 
      { 
    return PartialView("_error");} } 

它調用庫類中的以下保存方法: -

public void Save() { 
      entities1.SaveChanges(); 
     } 

我面臨以下兩個問題與上述代碼: -

  1. incase two system us ers在相同類別註冊同一個用戶,那麼在repository.Save();catch (System.Data.UpdateException ex)將不會處理它的DbUpdateException將引發以下異常。所以我如何強制將異常傳遞給我的操作方法?

  2. 如果發生異常而不是返回PartialView("_error");},我該如何返回一個警告框(表示用戶可能已經添加到此類)。

BR

回答

1

1)據我所看到的,DbUpdateException不是一個子類UpdateException的,所以如果你想趕上DbUpdateException你應該趕上明確。或者,您可以捕獲DataException,它是UpdateException和DbUpdateException的基類

相關問題