Person ProcessPerson(String iid, String surname, String name, Gender gender, int yearOfBirth) { if (String.IsNullOrEmpty(surname)) { throw new ArgumentNullException("surname"); } var surnameCheck = (surname.Replace('ё', 'е') + " " + (name ?? string.Empty).Replace('ё', 'е')); var gChar = gender.GetFirstLetter(); Person person; if (!string.IsNullOrEmpty(iid)) { person = Context.People.FirstOrDefault(p => p.Iid == iid); if (person == null) { person = Context.ClimbersOnCompetition.Where(p => p.Iid == iid).Select(p => p.Person).FirstOrDefault(); } if (person != null) { person.Surname = surname; person.Name = name; person.Gender = gender; if (person.YearOfBirth != yearOfBirth) { person.SetDateOfBirthByYear(yearOfBirth); } Context.SaveChanges(); return(person); } } person = Context.People.FirstOrDefault(p => (p.Surname + " " + p.Name) == surnameCheck && p.GenderChar == gChar && p.DateOfBirth.Year == yearOfBirth); if (person != null) { return(person); } person = Context.People.Add(new Person(Context) { ClimbersLicenses = new List <Climber>(), CompetitionAppearances = new List <ClimberOnCompetition>(), Gender = gender, Name = (name ?? string.Empty).Replace('ё', 'е'), Surname = surname.Replace('ё', 'е') }); person.SetDateOfBirthByYear(yearOfBirth); Context.SaveChanges(); return(Context.People.First(p => p.Iid == person.Iid)); }