public void Delete(DelegationSetting setting)
        {
            using (var entities = IdentityServerConfigurationContext.Get())
            {
                var record =
                    (from entry in entities.Delegation
                        where entry.UserName.Equals(setting.UserName, StringComparison.OrdinalIgnoreCase) &&
                              entry.Realm.Equals(setting.Realm.AbsoluteUri, StringComparison.OrdinalIgnoreCase)
                        select entry)
                        .Single();

                entities.Delegation.Remove(record);
                entities.SaveChanges();
            }
        }
        public void Add(DelegationSetting setting)
        {
            using (var entities = IdentityServerConfigurationContext.Get())
            {
                var entity = new Delegation
                {
                    UserName = setting.UserName,
                    Realm = setting.Realm.AbsoluteUri,
                    Description = setting.Description
                };

                entities.Delegation.Add(entity);
                entities.SaveChanges();
            }
        }
        public ActionResult Add(DelegationSetting model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    this.delegationRepository.Add(model);
                    TempData["Message"] = Resources.DelegationController.RealmAdded;
                    return RedirectToAction("Configure", new { id = model.UserName });
                }
                catch (ValidationException ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
                catch
                {
                    ModelState.AddModelError("", Resources.DelegationController.ErrorAddingDelegationSetting);
                }
            }

            var vm = new DelegationSettingsForUserViewModel(this.delegationRepository, this.userManagementRepository, model.UserName);
            return View("Configure", vm);
        }