public Employee Add(Employee employee) { Employee employeeModified = null; if (employee == null) { _logger.WriteLog("Invalid Record"); throw new Exception("Invalid Record"); } if (context.Employees.Count() == 0) { employeeModified = context.Employees.Add(employee); } else { var isExisting = context.Employees.FirstOrDefault(x => x.FName.ToUpper() == employee.FName.ToUpper() && x.LName.ToUpper() == employee.LName.ToUpper()); if (employee.Addresses != null) { string phoneNumber = employee.Addresses.FirstOrDefault().Phone.ToUpper(); var isPhoneExists = context.Addresses.FirstOrDefault(x => x.Phone.ToUpper() == phoneNumber); if (isExisting == null && isPhoneExists == null) { employeeModified = context.Employees.Add(employee); } else { if (isExisting != null) { _logger.WriteException("Exception Occured :", new FaultException("Duplicate Entry Found")); throw new FaultException("Duplicate Entry Found"); } if (isPhoneExists != null) { _logger.WriteException("Exception Occured :", new FaultException("Duplicate Phone Number Found")); throw new FaultException("Duplicate Phone Number Found"); } } } } context.SaveChanges(); return(employeeModified); }