2015-03-25 27 views
0

我收到此錯誤後點擊刪除:錯誤在刪除項目從MVC服務

try 
     { 
      var crews = await GetCrewsAttached(id); 
      var assetbookings = await _assetBooking.Get(id); 
      var parts = await GetParts("LTD", id, false); 
      //Checks if records exist in assetbookings and the employee not attached to any crew and doesnt have any parts associated to the employee 
      if (assetbookings == null && (crews.Count() == 0 || crews == null) && (parts == null || parts.ToList().Count() == 0)) 
      { 
       var employee = await _employeeRepository.Get(
        e => e.EmployeeID == id, 
        new List<Expression<Func<gblEmployee, object>>>() { 
        (e => e.Address), 
        (e => e.EmployeeTraining.Select(t=>t.Training)), 
        (e => e.EmployeeWorkSchedule) 
       }); 

       if (employee != null && employee.IsSupervisor != true) 
       { 
        foreach (var training in employee.EmployeeTraining) 
        { 
         _employeeTrainingRepository.Delete(training); 
        } 
        await _employeeTrainingRepository.Commit(); 


        if (employee.Address != null) 
        { 
         _addressRepository.Delete(employee.Address); 
         await _addressRepository.Commit(); 
        } 
        _employeeRepository.Delete(employee); 
        await _employeeRepository.Commit(); 


       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new NexgenException(ex); 
     } 

我在await _employeeRepository.Commit();

任何方法得到錯誤化解? 錯誤是操作失敗:該關係無法更改,因爲一個或多個外鍵屬性是不可空的。當對關係進行更改時,相關的外鍵屬性將設置爲空值。如果外鍵不支持空值,則必須定義新的關係,必須爲外鍵屬性指定另一個非空值,或者必須刪除無關對象。

回答

0

嘗試調用.SaveChanges承諾。你也可以創建一個catch塊來捕獲DbEntityValidationException,這樣你就可以檢查缺失值

相關問題