private void AddCeDatabase(DatabaseType dbType)
 {
     try
     {
         var dialog = new ConnectionDialog();
         string path;
         if (TryGetInitialPath(package, out path))
         {
             dialog.InitialPath = path;                
         }
         dialog.DbType = dbType;
         dialog.ShowDDEXInfo = package.VSSupportsSimpleDDEX4Provider() || package.VSSupportsSimpleDDEX35Provider();
         dialog.CouldSupportPrivateProvider =
             (dbType == DatabaseType.SQLCE40 && (SqlCeToolboxPackage.VisualStudioVersion == new Version(12, 0) || SqlCeToolboxPackage.VisualStudioVersion == new Version(14, 0)) )
             || (dbType == DatabaseType.SQLCE35 && package.VSSupportsEF6());
         bool? result = dialog.ShowModal();
         if (result.HasValue && result.Value == true)
         {
             if (!string.IsNullOrWhiteSpace(dialog.ConnectionString))
             {
                 Helpers.DataConnectionHelper.SaveDataConnection(dialog.ConnectionString, dbType, package);
                 ExplorerControl control = _parentWindow.Content as ExplorerControl;
                 control.BuildDatabaseTree();
                 Helpers.DataConnectionHelper.LogUsage("DatabasesAddCeDatabase");
             }
         }
     }
     catch (Exception ex)
     {
         Helpers.DataConnectionHelper.SendError(ex, dbType);
     }
 }
        public void SyncFxGenerateLocalDatabaseCacheCode(object sender, ExecutedRoutedEventArgs e)
        {
            // http://www.mztools.com/articles/2007/MZ2007011.aspx
            var menuItem = sender as MenuItem;
            if (menuItem == null) return;

            Scope scope = (Scope)menuItem.Tag;

            var dte = package.GetServiceHelper(typeof(EnvDTE.DTE)) as EnvDTE.DTE;

            var dteH = new Helpers.EnvDTEHelper();

            var project = dteH.GetProject(dte);
            if (project == null)
            {
                EnvDTEHelper.ShowError("Please select a project in Solution Explorer, where you want the SyncFx classes to be placed");
                return;
            }
            if (!dteH.AllowedProjectKinds.Contains(new Guid(project.Kind)))
            {
                EnvDTEHelper.ShowError("The selected project type does not support Sync Framework (please let me know if I am wrong)");
                return;
            }
            if (project.CodeModel.Language != CodeModelLanguageConstants.vsCMLanguageCSharp)
            {
                EnvDTEHelper.ShowError("Unsupported code language, only C# is currently supported");
                return;
            }
            if (project.Properties.Item("TargetFrameworkMoniker") == null)
            {
                EnvDTEHelper.ShowError("The selected project type does not support Sync Framework - missing TargetFrameworkMoniker");
                return;
            }
            if (!project.Properties.Item("TargetFrameworkMoniker").Value.ToString().Contains(".NETFramework"))
            {
                EnvDTEHelper.ShowError("The selected project type does not support .NET Desktop - wrong TargetFrameworkMoniker: " + project.Properties.Item("TargetFrameworkMoniker").Value.ToString());
                return;
            }

            try
            {
                string serverConnectionString = DataConnectionHelper.PromptForConnectionString(package);
                if (!string.IsNullOrEmpty(serverConnectionString))
                {
                    string clientConnectionString = string.Empty;

                    //grab target SQL CE Database
                    var dialog = new ConnectionDialog();
                    dialog.DbType = DatabaseType.SQLCE35;
                    bool? result = dialog.ShowModal();
                    if (result.HasValue && result.Value == true && !string.IsNullOrWhiteSpace(dialog.ConnectionString))
                    {
                        clientConnectionString = dialog.ConnectionString;
                    }
                    else
                    {
                        return;
                    }

                    string model = string.Empty;
                    SyncFxDialog sfd = new SyncFxDialog();
                    int totalCount = 0;
                    var databaseInfo = new DatabaseMenuCommandParameters
                    {
                        DatabaseInfo = new DatabaseInfo
                        {
                            ConnectionString = serverConnectionString,
                            DatabaseType = DatabaseType.SQLServer
                        }
                    };
                    totalCount = SyncFxGetObjectsForSync(sfd, databaseInfo);
                    sfd.ModelName = model;

                    bool? res = sfd.ShowModal();
                    if (res.HasValue && res.Value == true && (sfd.Tables.Count > 0) && !string.IsNullOrWhiteSpace(sfd.ModelName))
                    {
                        model = sfd.ModelName;
                        var defaultNamespace = project.Properties.Item("DefaultNamespace").Value.ToString();

                        var columns = sfd.Columns.Where(c => sfd.Tables.Contains(c.TableName)).ToList();
                        var classes = new SyncFxHelper().GenerateCodeForScope(serverConnectionString, clientConnectionString, "SQL", model, columns, defaultNamespace);
                        var projectPath = project.Properties.Item("FullPath").Value.ToString();

                        foreach (var item in classes)
                        {
                            string fileName = Path.Combine(projectPath, item.Key + ".cs");
                            if (File.Exists(fileName))
                            {
                                File.Delete(fileName);
                            }
                            File.WriteAllText(fileName, item.Value);
                            project.ProjectItems.AddFromFile(fileName);
                        }
                        //Adding references - http://blogs.msdn.com/b/murat/archive/2008/07/30/envdte-adding-a-refernce-to-a-project.aspx
                        EnvDTEHelper.AddReference(project, "System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");

                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization, Version=2.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data.SqlServer, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.AddReference(project, "Microsoft.Synchronization.Data.SqlServerCe, Version=3.1.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91");
                        EnvDTEHelper.ShowMessage("Scope: " + model + " code generated.");
                        Helpers.DataConnectionHelper.LogUsage("DatabasesSyncAddLocalDBCache");
                    }
                }
            }
            catch (Exception ex)
            {
                Helpers.DataConnectionHelper.SendError(ex, DatabaseType.SQLServer);
            }
        }