简体   繁体   English

从MVC服务删除项目时出错

[英]Error While Deleting Item From MVC Service

I am getting this error after click Delete: 单击删除后出现此错误:

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

I am getting Error at await _employeeRepository.Commit(); 我在await _employeeRepository.Commit();时收到错误await _employeeRepository.Commit();

Any Method to Resolve ? 有解决的方法吗? Error is The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. 错误是操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系。 When a change is made to a relationship, the related foreign-key property is set to a null value. 对关系进行更改时,相关的外键属性将设置为空值。 If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted 如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象

Try to call .SaveChanges before the commit. 尝试在提交之前调用.SaveChanges。 Also you can create a catch block that catches a DbEntityValidationException so you can check for missing values 您还可以创建一个捕获DbEntityValidationException的catch块,以便检查缺少的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM