src/Misc/EFTools/EntityDesign/VisualStudio/ModelWizard/engine/updatefromdatabaseengine.cs
// <summary>
//     Updates the EDMX file based on the Database changes
// </summary>
Updates the EDMX file based on the Database changes
Updates the EDMX file based on the Database changes
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        [SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        internal static void UpdateModelFromDatabase(EntityDesignArtifact artifact)
        {
            VsUtils.EnsureProvider(artifact);
            var serviceProvider = Services.ServiceProvider;
            var project = VSHelpers.GetProjectForDocument(artifact.Uri.LocalPath, serviceProvider);
            ModelBuilderWizardForm.WizardMode startMode;
            var settings =
                SetupSettingsAndModeForDbPages(
                    serviceProvider, project, artifact, true,
                    ModelBuilderWizardForm.WizardMode.PerformDatabaseConfigAndSelectTables,
                    ModelBuilderWizardForm.WizardMode.PerformSelectTablesOnly, out startMode);
            settings.WizardKind = WizardKind.UpdateModel;
            if (null != artifact.StorageModel()
                && null != artifact.StorageModel().Namespace
                && !string.IsNullOrEmpty(artifact.StorageModel().Namespace.Value))
            {
                settings.StorageNamespace = artifact.StorageModel().Namespace.Value;
            }
            if (null != artifact.ConceptualModel()
                && null != artifact.ConceptualModel().Namespace
                && !string.IsNullOrEmpty(artifact.ConceptualModel().Namespace.Value))
            {
                settings.ModelNamespace = artifact.ConceptualModel().Namespace.Value;
            }
            settings.ModelBuilderEngine = new UpdateModelFromDatabaseModelBuilderEngine();
            var form = new ModelBuilderWizardForm(serviceProvider, settings, startMode);
            try
            {
                form.Start();
            }
            catch (Exception e)
            {
                VsUtils.ShowErrorDialog(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.ModelObjectItemWizard_UnexpectedExceptionHasOccurred,
                        e.Message));
                return;
            }
            if (form.WizardCancelled
                || !form.WizardFinished)
            {
                return;
            }
            ConfigFileHelper.UpdateConfig(settings);
            var editingContext = PackageManager.Package.DocumentFrameMgr.EditingContextManager.GetNewOrExistingContext(artifact.Uri);
            var shouldReloadArtifact = ProcessAccumulatedInfo(editingContext, artifact, settings);
            if (startMode == ModelBuilderWizardForm.WizardMode.PerformDatabaseConfigAndSelectTables)
            {
                var edmxItem = VsUtils.GetProjectItemForDocument(artifact.Uri.LocalPath, serviceProvider);
                new DbContextCodeGenerator().AddDbContextTemplates(edmxItem, settings.UseLegacyProvider);
            }
            if (shouldReloadArtifact)
            {
                artifact.ReloadArtifact();
                artifact.IsDirty = true;
            }
        }
[ SuppressMessage ( " " , " " ) ] [ SuppressMessage ( " " , " " ) ] [ SuppressMessage ( " " , " " ) ] internal static void UpdateModelFromDatabase ( EntityDesignArtifact artifact ) { VsUtils . EnsureProvider ( artifact ) ; var serviceProvider = Services . ServiceProvider ; var project = VSHelpers . GetProjectForDocument ( artifact . Uri . LocalPath , serviceProvider ) ; ModelBuilderWizardForm . WizardMode startMode ; var settings = SetupSettingsAndModeForDbPages ( serviceProvider , project , artifact , true , ModelBuilderWizardForm . WizardMode . PerformDatabaseConfigAndSelectTables , ModelBuilderWizardForm . WizardMode . PerformSelectTablesOnly , out startMode ) ; settings . WizardKind = WizardKind . UpdateModel ; if ( null != artifact . StorageModel ( ) && null != artifact . StorageModel ( ) . Namespace && ! string . IsNullOrEmpty ( artifact . StorageModel ( ) . Namespace . Value ) ) { settings . StorageNamespace = artifact . StorageModel ( ) . Namespace . Value ; } if ( null != artifact . ConceptualModel ( ) && null != artifact . ConceptualModel ( ) . Namespace && ! string . IsNullOrEmpty ( artifact . ConceptualModel ( ) . Namespace . Value ) ) { settings . ModelNamespace = artifact . ConceptualModel ( ) . Namespace . Value ; } settings . ModelBuilderEngine = new UpdateModelFromDatabaseModelBuilderEngine ( ) ; var form = new ModelBuilderWizardForm ( serviceProvider , settings , startMode ) ; try { form . Start ( ) ; } catch ( Exception e ) { VsUtils . ShowErrorDialog ( string . Format ( CultureInfo . CurrentCulture , Resources . ModelObjectItemWizard_UnexpectedExceptionHasOccurred , e . Message ) ) ; return ; } if ( form . WizardCancelled || ! form . WizardFinished ) { return ; } ConfigFileHelper . UpdateConfig ( settings ) ; var editingContext = PackageManager . Package . DocumentFrameMgr . EditingContextManager . GetNewOrExistingContext ( artifact . Uri ) ; var shouldReloadArtifact = ProcessAccumulatedInfo ( editingContext , artifact , settings ) ; if ( startMode == ModelBuilderWizardForm . WizardMode . PerformDatabaseConfigAndSelectTables ) { var edmxItem = VsUtils . GetProjectItemForDocument ( artifact . Uri . LocalPath , serviceProvider ) ; new DbContextCodeGenerator ( ) . AddDbContextTemplates ( edmxItem , settings . UseLegacyProvider ) ; } if ( shouldReloadArtifact ) { artifact . ReloadArtifact ( ) ; artifact . IsDirty = true ; } }
Updates the EDMX file based on the Database changes
Updates the EDMX file based on the Database changes
// set up ModelBuilderSettings for startMode=PerformDatabaseConfigAndSelectTables // use existing storage namespace as new storage namespace // use existing model namespace as new model namespace (this only affects the temporary // artifact but there is a situation where the C-side EntityContainer has been given the // same name as the default model namespace where not setting this causes the temporary // artifact to be unreadable because of symbol clashes) // call the ModelBuilderWizardForm // if Wizard was cancelled or the user hit 'X' to close window // no need for any further action // Update the app. or web.config, register build providers etc // use form.ModelBuilderSettings to look at accumulated info and // take appropriate action // If database was configured, add DbContext templates // We can reload only after we added EF references to the project otherwise we would get a watermark // saying that the schema version does not match the referenced EF version which would not be true. // If we reload becuase there was an extension that potentially modified the artifact then it does not matter.
artifact
EntityDesignArtifact