private TestApplication Patch(TestApplication testApplication)
        {
            var result = StoredTestApplications.Find(testApplication.Id) ?? StoredTestApplications.Add(testApplication);
            if (!result.IsPatched && !result.IsPatching)
            {
                result.IsPatching = true;
                try
                {
                    var selectedApplication = GetApplicationsById(result.ApplicationId)
                        .Select(Patch)
                        .FirstOrDefault();
                    result.Application = selectedApplication;

                    var selectedTestEnvironment = GetTestEnvironmentsById(result.TestEnvironmentId)
                        .Select(Patch)
                        .FirstOrDefault();
                    result.TestEnvironment = selectedTestEnvironment;

                    result.IsPatched = true;
                }
                finally
                {
                    result.IsPatching = false;
                }
            }

            return result;
        }
        /// <summary>
        /// Adds a new test application.
        /// </summary>
        /// <param name="application">The new test application.</param>
        public void Add(TestApplication application)
        {
            VerifySchemaVersion();

            var result = StoredTestApplications.Add(application);
            Patch(result);
        }
        /// <summary>
        /// Updates the given application.
        /// </summary>
        /// <param name="application">The test application that should be updated.</param>
        public void Update(TestApplication application)
        {
            VerifySchemaVersion();

            var storedApplication = TestApplication(application.Id);
            if (storedApplication != null)
            {
                if (!ReferenceEquals(storedApplication, application))
                {
                    var shouldPatch = false;
                    if (storedApplication.fk_ApplicationId != application.fk_ApplicationId)
                    {
                        storedApplication.fk_ApplicationId = application.fk_ApplicationId;
                        storedApplication.Application = null;
                        shouldPatch = true;
                    }

                    if (storedApplication.fk_TestEnvironmentId != application.fk_TestEnvironmentId)
                    {
                        storedApplication.fk_TestEnvironmentId = application.fk_TestEnvironmentId;
                        storedApplication.TestEnvironment = null;
                        shouldPatch = true;
                    }

                    if (shouldPatch)
                    {
                        Patch(storedApplication);
                    }
                }

                var entry = Entry(storedApplication);
                entry.State = EntityState.Modified;
            }
        }
        private void SetApplicationsForEnvironment(XDocument xmlContent, int environmentId)
        {
            // Expecting the following
            // <applications>
            //     <application name="{APPLICATION_NAME}"
            //                  version="{APPLICATION_VERSION}" />
            //
            //      .... more applications here ...
            //
            // </applications>
            var applicationsNode = xmlContent.Descendants("applications").First();
            foreach (var applicationNode in applicationsNode.Descendants("application"))
            {
                var name = applicationNode.Attribute("name").Value;
                var version = new Version(applicationNode.Attribute("version").Value);
                var application = new ApplicationDescription
                    {
                        Name = name,
                        Version = version,
                    };

                var storedApplicationId = m_Context.Applications()
                    .Where(a => a.IsNewerOrEqualVersionOf(application))
                    .OrderBy(a => a.Version)
                    .Select(a => a.Id)
                    .First();

                var testApplication = new TestApplication
                    {
                        ApplicationId = storedApplicationId,
                        TestEnvironmentId = environmentId,
                    };

                m_Context.Add(testApplication);
                m_Context.StoreChanges();
            }
        }