internal static void UpdateModelFromDatabase(EntityDesignArtifact artifact) { VsUtils.EnsureProvider(artifact); var serviceProvider = Services.ServiceProvider; var project = VSHelpers.GetProjectForDocument(artifact.Uri.LocalPath, serviceProvider); // set up ModelBuilderSettings for startMode=PerformDatabaseConfigAndSelectTables ModelBuilderWizardForm.WizardMode startMode; var settings = SetupSettingsAndModeForDbPages( serviceProvider, project, artifact, true, ModelBuilderWizardForm.WizardMode.PerformDatabaseConfigAndSelectTables, ModelBuilderWizardForm.WizardMode.PerformSelectTablesOnly, out startMode); settings.WizardKind = WizardKind.UpdateModel; // use existing storage namespace as new storage namespace if (null != artifact.StorageModel() && null != artifact.StorageModel().Namespace && !string.IsNullOrEmpty(artifact.StorageModel().Namespace.Value)) { settings.StorageNamespace = artifact.StorageModel().Namespace.Value; } // 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) if (null != artifact.ConceptualModel() && null != artifact.ConceptualModel().Namespace && !string.IsNullOrEmpty(artifact.ConceptualModel().Namespace.Value)) { settings.ModelNamespace = artifact.ConceptualModel().Namespace.Value; } settings.ModelBuilderEngine = new UpdateModelFromDatabaseModelBuilderEngine(); // call the ModelBuilderWizardForm 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 Wizard was cancelled or the user hit 'X' to close window // no need for any further action if (form.WizardCancelled || !form.WizardFinished) { return; } // Update the app. or web.config, register build providers etc ConfigFileHelper.UpdateConfig(settings); // use form.ModelBuilderSettings to look at accumulated info and // take appropriate action var editingContext = PackageManager.Package.DocumentFrameMgr.EditingContextManager.GetNewOrExistingContext(artifact.Uri); var shouldReloadArtifact = ProcessAccumulatedInfo(editingContext, artifact, settings); // If database was configured, add DbContext templates if (startMode == ModelBuilderWizardForm.WizardMode.PerformDatabaseConfigAndSelectTables) { var edmxItem = VsUtils.GetProjectItemForDocument(artifact.Uri.LocalPath, serviceProvider); new DbContextCodeGenerator().AddDbContextTemplates(edmxItem, settings.UseLegacyProvider); } // 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. if (shouldReloadArtifact) { artifact.ReloadArtifact(); artifact.IsDirty = true; } }
internal static void GenerateDatabaseScriptFromModel(EntityDesignArtifact artifact) { VsUtils.EnsureProvider(artifact); var project = VSHelpers.GetProjectForDocument(artifact.Uri.LocalPath, PackageManager.Package); var sp = Services.ServiceProvider; ModelBuilderWizardForm form; ModelBuilderWizardForm.WizardMode startMode; ModelBuilderSettings settings; // Start the hourglass, especially because we'll be incurring a perf hit from validating using (new VsUtils.HourglassHelper()) { // Before running the Generate Database wizard, we have to make sure that the C-Side validates VisualStudioEdmxValidator.LoadAndValidateFiles(artifact.Uri); if ( artifact.ArtifactSet.GetAllErrors() .Count(ei => ei.ErrorClass == ErrorClass.Runtime_CSDL || ei.ErrorClass == ErrorClass.Escher_CSDL) > 0) { VsUtils.ShowErrorDialog(Resources.DatabaseCreation_ValidationFailed); return; } // set up ModelBuilderSettings settings = SetupSettingsAndModeForDbPages( sp, project, artifact, false, ModelBuilderWizardForm.WizardMode.PerformDatabaseConfigAndDBGenSummary, ModelBuilderWizardForm.WizardMode.PerformDBGenSummaryOnly, out startMode); form = new ModelBuilderWizardForm(sp, settings, startMode); } var originalSchemaVersion = settings.TargetSchemaVersion; try { // start the ModelBuilderWizardForm; this will start the workflow in another thread. form.Start(); } catch (Exception e) { VsUtils.ShowErrorDialog( String.Format( CultureInfo.CurrentCulture, Resources.ModelObjectItemWizard_UnexpectedExceptionHasOccurred, e.Message)); return; } // if Wizard was cancelled or the user hit 'X' to close window // no need for any further action if (form.WizardCancelled || !form.WizardFinished) { return; } // If database was configured, add DbContext templates if (startMode == ModelBuilderWizardForm.WizardMode.PerformDatabaseConfigAndDBGenSummary) { var edmxItem = VsUtils.GetProjectItemForDocument(artifact.Uri.LocalPath, sp); new DbContextCodeGenerator().AddDbContextTemplates(edmxItem, settings.UseLegacyProvider); // We need to reload the artifact if we updated the edmx as part of generating // model from the database. if (settings.TargetSchemaVersion != originalSchemaVersion) { artifact.ReloadArtifact(); } } }
internal static void DoMigrate(CommandProcessorContext cpc, EntityDesignArtifact artifact) { var xmlModelProvider = artifact.XmlModelProvider as VSXmlModelProvider; Debug.Assert(xmlModelProvider != null, "Artifact's model provider is not type of VSXmlModelProvider."); if ((xmlModelProvider != null) && (xmlModelProvider.UndoManager != null)) { var undoManager = xmlModelProvider.UndoManager; try { // We need to temporarily disable the Undo Manager because this operation is not undoable. if (undoManager != null) { undoManager.Enable(0); } var command = new MigrateDiagramInformationCommand(artifact); var processor = new CommandProcessor(cpc, shouldNotifyObservers: false); processor.EnqueueCommand(command); processor.Invoke(); Debug.Assert(artifact.DiagramArtifact != null, "Diagram artifact should have been created by now."); if (artifact.DiagramArtifact != null) { // Ensure that diagram file is added to the project. var service = PackageManager.Package.GetService(typeof(DTE)) as DTE; service.ItemOperations.AddExistingItem(artifact.DiagramArtifact.Uri.LocalPath); // Reload the artifacts. artifact.ReloadArtifact(); artifact.IsDirty = true; // The code below ensures mapping window and model-browser window are refreshed. Debug.Assert( PackageManager.Package.DocumentFrameMgr != null, "Could not find the DocumentFrameMgr for this package"); if (PackageManager.Package.DocumentFrameMgr != null) { PackageManager.Package.DocumentFrameMgr.SetCurrentContext(cpc.EditingContext); } } } finally { if (undoManager != null) { undoManager.Enable(1); } } } }