/// <summary>
        /// 當整個專案正在進行 Generator 時所引發的事件.
        /// </summary>
        /// <param name="project">Visual Studio IDE 工具中,目前專案 Projects 集合</param>
        public void ProjectFinishedGenerating(Project project)
        {
            ProjectItem folder = null;
            ProjectItem viewFolder = null;

            var result = from item in project.ProjectItems.OfType<ProjectItem>().AsEnumerable()
                         where item.Name=="Models"
                         select item;

            if (result.FirstOrDefault() == null)
            {
                folder = project.ProjectItems.AddFolder("Models");
            }
            else
            {
                //若這個目錄已經存在,則直接取得這個目錄.
                folder = result.FirstOrDefault();
            }

            var resultView = folder.ProjectItems.OfType<ProjectItem>().Where(c => c.Name == "ViewModels");
            if(resultView.FirstOrDefault()==null)
            {
                viewFolder = folder.ProjectItems.AddFolder("ViewModels");
            }
            else
            {
                viewFolder = resultView.FirstOrDefault();
            }

            foreach (string node in frmORMappingWindow.SelectedTables)
            {
                //MessageBox.Show(string.Format("node={0}", node));
                string ClassName = node.Replace(" ", "_");
                //MessageBox.Show(string.Format("ClassName={0}", ClassName));
                ClassDef clsDef = new ClassDef();
                SQLStore store = new SQLStore();
                string ClassDefined = ClassDef.GetClassTemplate;
                ClassDefined = ClassDefined.Replace("$(NAMESPACE_DEF)$", string.Format("{0}.Models.ViewModels", project.Name));
                ClassDefined = ClassDefined.Replace("$(CLASS_DEF)$", clsDef.GetClassDef(store.GetNoDataDataTableByName(string.Format("[{0}]", node)), ClassName));
                //產生等會使用的暫存檔名
                string TempCSPath = Path.Combine(
                    Environment.GetEnvironmentVariable("temp"),
                    string.Format("{0}.cs", ClassName));

                //MessageBox.Show(TempCSPath);

                if(!Directory.Exists(Path.GetDirectoryName(TempCSPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(TempCSPath));
                }
                //建立暫存的 Class 檔案
                CreateModelCSFile(ClassDefined, TempCSPath);
                //加入暫存的 Class 檔案
                viewFolder.ProjectItems.AddFromFileCopy(TempCSPath);
                //刪除掉暫存檔案
                try {
                    File.Delete(TempCSPath);
                }
                catch (Exception ex) { } //刪除暫存檔案若失敗不處理任何訊息.
            }

            #region 使用預設Table建立View的資料夾
            ProjectItem defViewFolder = null;

            var result2 = from item in project.ProjectItems.OfType<ProjectItem>().AsEnumerable()
                         where item.Name == "Views"
                         select item;

            if (result2.FirstOrDefault() != null)
            {
                var defualtView = result2.FirstOrDefault();
                defViewFolder = defualtView.ProjectItems.AddFolder(defaultTable);
            }
            #endregion
        }
Esempio n. 2
0
 private void GetAndListColumnsByTable()
 {
     SQLStore store = new SQLStore();
     DataTable dt = store.GetNoDataDataTableByName(frmSettings.SelectedTableName);
     lbxColumns.ValueMember = "ColumnName";
     lbxColumns.DisplayMember = "ColumnName";
     SelectedTableColumns = dt.Columns.OfType<DataColumn>().AsEnumerable();
     lbxColumns.DataSource = SelectedTableColumns.ToList();
 }