DBContext自定义验证

本文关键字:验证 自定义 DBContext | 更新日期: 2023-09-27 18:02:04

对于DBContext来说相对较新,并且试图在DBContext 中编写自定义验证,而没有实现IValidatableObject接口或ValidateEntity重写。

。部门实体有:名字Trasit #BLID

下面是保存逻辑的一个片段:

public static Boolean saveDepartmentDetails(Department dept, out string errorMessage)
{
Boolean isSuccessful = false;
Int32 iRetVal = 0;
errorMessage = string.Empty;
using (OnlineFormsEntities context = new OnlineFormsEntities())
{
    if (dept != null)
    {
        if (dept.DepartmentID > 0)
        {
            //Todo: I know this is wrong.
            if (context.Departments.Where(d => d.DepartmentName.ToLower() == dept.DepartmentName.ToLower() && d.DepartmentID != dept.DepartmentID).Count() > 0)
            {
                errorMessage = "A Department with the given name already exists.";
                return false;
            }
            if (context.Departments.Where(d => d.Transit.ToLower() == dept.Transit.ToLower() && d.DepartmentID != dept.DepartmentID).Count() > 0)
            {
                errorMessage = "A Department with the given Transit # already exists.";
                return false;
            }
            context.Departments.Attach(dept);
            context.Entry(dept).State = EntityState.Modified;
        }
        else
        {
            if (context.Departments.Where(d => d.DepartmentName.ToLower() == dept.DepartmentName.ToLower()).Count() > 0)
            {
                errorMessage = "A Department with the given name already exists.";
                return false;
            }
            if (context.Departments.Where(d => d.Transit.ToLower() == dept.Transit.ToLower()).Count() > 0)
            {
                errorMessage = "A Department with the given Transit # already exists.";
                return false;
            }
            context.Departments.Add(dept);
        }
        try
        {
            iRetVal = context.SaveChanges();
        }
        catch (System.Data.OptimisticConcurrencyException opEx)
        {
            errorMessage = opEx.Message;
            ((IObjectContextAdapter)context).ObjectContext.Refresh(RefreshMode.ClientWins, dept);
            iRetVal = context.SaveChanges();
        }
        isSuccessful = (iRetVal > 0) ? true : false;
    }
}
return isSuccessful;
}

我需要更新逻辑的帮助。或者用更好的方式来编码SaveDepartmentDetails

DBContext自定义验证

我建议将所有公共逻辑拉出到您正在保存的同一类中的验证方法中(即返回bool值的方法:如果通过则为True,如果不通过则为False)。您也可以将错误消息用作out参数):

private bool ValidateDept(OnlineFormsEntities context, Department dept, out string errorMessage)
{
  errorMessage = string.empty;
  if (context.Departments.Any(d => d.DepartmentName.ToLower() == dept.DepartmentName.ToLower() && d.ID != dept.ID))
   {
     errorMessage = "A Department with the given name already exists.";
     return false;
   }
 if (context.Departments.Any(d => d.Transit.ToLower() == dept.Transit.ToLower() && d.ID != dept.ID))
   {
     errorMessage = "A Department with the given Transit # already exists.";
     return false;
  }
 return true;
}

这样做,那么,将允许您在保存方法的开始调用该验证方法,然后只在它通过后才执行添加/附加。