public Application Execute(IEditApplicationModel model)
        {
            var application = _context.Applications
                              .Include(a => a.Vendor)
                              .Include(a => a.ApplicationEducationOrganizations)
                              .Include(a => a.ApiClients)
                              .Include(a => a.Profiles)
                              .SingleOrDefault(a => a.ApplicationId == model.ApplicationId);

            if (application == null)
            {
                throw new NotFoundException <int>("application", model.ApplicationId);
            }

            if (application.Vendor.IsSystemReservedVendor())
            {
                throw new Exception("This Application is required for proper system function and may not be modified");
            }

            var newVendor  = _context.Vendors.Single(v => v.VendorId == model.VendorId);
            var newProfile = model.ProfileId.HasValue
                ? _context.Profiles.Single(p => p.ProfileId == model.ProfileId.Value)
                : null;

            var apiClient = application.ApiClients.Single();

            apiClient.Name = model.ApplicationName;

            application.ApplicationName = model.ApplicationName;
            application.ClaimSetName    = model.ClaimSetName;
            application.Vendor          = newVendor;

            if (application.ApplicationEducationOrganizations == null)
            {
                application.ApplicationEducationOrganizations = new Collection <ApplicationEducationOrganization>();
            }

            application.ApplicationEducationOrganizations.Clear();
            model.EducationOrganizationIds.ToList().ForEach(id => application.ApplicationEducationOrganizations.Add(application.CreateApplicationEducationOrganization(id)));

            if (application.Profiles == null)
            {
                application.Profiles = new Collection <Profile>();
            }

            application.Profiles.Clear();

            if (newProfile != null)
            {
                application.Profiles.Add(newProfile);
            }

            _context.SaveChanges();
            return(application);
        }
        public void Execute(IEditApplicationModel model)
        {
            var application = _context.Applications
                              .Include(a => a.Vendor)
                              .Include(a => a.ApplicationEducationOrganizations)
                              .Include(a => a.ApiClients)
                              .Include(a => a.Profiles)
                              .Single(a => a.ApplicationId == model.ApplicationId);

            var newVendor          = _context.Vendors.Single(v => v.VendorId == model.VendorId);
            var newApplicationName = CloudOdsApplicationName.GetPersistedName(model.ApplicationName, model.Environment);
            var newProfile         = model.ProfileId.HasValue
                ? _context.Profiles.Single(p => p.ProfileId == model.ProfileId.Value)
                : null;

            var apiClient = application.ApiClients.Single();

            apiClient.Name = newApplicationName;

            application.ApplicationName = newApplicationName;
            application.ClaimSetName    = model.ClaimSetName;
            application.Vendor          = newVendor;

            if (application.ApplicationEducationOrganizations == null)
            {
                application.ApplicationEducationOrganizations = new Collection <ApplicationEducationOrganization>();
            }

            application.ApplicationEducationOrganizations.Clear();
            model.EducationOrganizationIds.ToList().ForEach(id => application.ApplicationEducationOrganizations.Add(application.CreateApplicationEducationOrganization(id)));

            if (application.Profiles == null)
            {
                application.Profiles = new Collection <Profile>();
            }

            application.Profiles.Clear();

            if (newProfile != null)
            {
                application.Profiles.Add(newProfile);
            }

            _context.SaveChanges();
        }