public void changes_primary_language()
        {
            //setup
            var request = new UpdateProviderLanguageRequest();
            request.Id = _existingNonPrimaryProviderLanguageId;
            request.IsPrimary = true;

            var handler = CreateHandler();

            //act
            var response = HandleRequest<UpdateProviderLanguageResponse>(handler, request);

            //assert
            using (var context = _objectContextFactory.CreateObjectContext())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                context.ContextOptions.ProxyCreationEnabled = true;
                var provider = context.CreateObjectSet<Provider>().Single(p => p.Id == _providerIdWithLanguage);

                var otherLanguage = provider.ProviderLanguages.Single(ps => ps.Id == _existingProviderLanguageId);
                Assert.IsFalse(otherLanguage.IsPrimary);

                var updatedLanguage = provider.ProviderLanguages.Single(ps => ps.Id == _existingNonPrimaryProviderLanguageId);
                Assert.IsTrue(updatedLanguage.IsPrimary);
            }
        }
        public static void Save(ProviderAdminDetailView parent, SubEntity providerLanguages, int providerId)
        {
            try
            {
                providerLanguages.ErrorMessage = string.Empty;

                if (providerLanguages.Records.Count(s => bool.Parse(parent.GetFieldValue(s.Fields, _isPrimary))) > 1)
                {
                    throw new BusinessException("Only one Provider Language can be set as the primary language");
                }

                //Edits
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.Edited))
                {
                    var request = new UpdateProviderLanguageRequest();

                    request.Id = int.Parse(parent.GetFieldValue(record.Fields, _id));
                    request.IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary));
                    request.IsFluent = bool.Parse(parent.GetFieldValue(record.Fields, _isFluent));

                    parent.ProcessRequest<UpdateProviderLanguageResponse>(request);
                }

                //Existing Records Added
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.Added))
                {
                    var request = new AddProviderLanguageRequest()
                    {
                        ProviderId = providerId,
                        LanguageId = int.Parse(parent.GetFieldValue(record.Fields, _languageId)),
                        IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary)),
                        IsFluent = bool.Parse(parent.GetFieldValue(record.Fields, _isFluent))
                    };

                    parent.ProcessRequest<AddProviderLanguageResponse>(request);
                }

                //New Records Created
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.New))
                {
                    //Add New Language
                    var addLanguageRequest = new AddLanguageRequest()
                    {
                        Name = parent.GetFieldValue(record.Fields, _languageName),
                        IsEnabled = true
                    };
                    var addedLanguageId = parent.ProcessRequest<AddLanguageResponse>(addLanguageRequest).Id;

                    //Add newly created language to provider
                    var request = new AddProviderLanguageRequest()
                    {
                        ProviderId = providerId,
                        LanguageId = addedLanguageId,
                        IsPrimary = bool.Parse(parent.GetFieldValue(record.Fields, _isPrimary)),
                        IsFluent = bool.Parse(parent.GetFieldValue(record.Fields, _isFluent))
                    };

                    parent.ProcessRequest<AddProviderLanguageResponse>(request);
                }

                //Deletes
                foreach (var record in providerLanguages.Records.Where(r => r.Action == RecordActions.Deleted))
                {
                    var request = new DeleteProviderLanguageRequest()
                    {
                        ProviderId = providerId,
                        LanguageId = int.Parse(parent.GetFieldValue(record.Fields, _languageId))
                    };
                    parent.ProcessRequest<DeleteProviderLanguageResponse>(request);
                }
            }
            catch (Exception ex)
            {
                providerLanguages.ErrorMessage = ex.Message;
                throw;
            }

            providerLanguages = BuildLanguagesSubEntity(parent, providerId);
        }