public SettingItem InsertSetting(SettingItem item) { bool deleteResult = DeleteSettings(); if (deleteResult) { Setting dbItem = new Setting { SelectionName = item.SelectionName, SelectionDate = item.SelectionDate, StartTime = item.StartTime, FinishTime = item.FinishTime, SelectionCount = item.SelectionCount, ParticipantCount = item.ParticipantCount, SelectionStatus = SettingStatus.InPreparation }; db.Settings.RemoveRange(db.Settings); db.Settings.Add(dbItem); CredentialService credentialService = new CredentialService(db); var dictionary = credentialService.GenerateCredentials((int)item.ParticipantCount); for (var i = 0; i < item.ParticipantCount; i++) { Credential credential = new Credential { SettingID = item.SettingID, Password = dictionary.ElementAt(i).Key, Status = CredentialStatus.NoSelection, Setting = dbItem }; db.Credentials.Add(credential); } db.SaveChanges(); item.SettingID = dbItem.SettingID; return(item); } else { return(null); } }