日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

图解CodeSmith使用和实用教程一 - 入门和生成MIS项目实体层代码

發(fā)布時間:2025/4/14 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图解CodeSmith使用和实用教程一 - 入门和生成MIS项目实体层代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

? ? CodeSmith,也就是傳說中的那個代碼生成工具。

? ? 下載CodeSmith 6.5,可到我網(wǎng)盤下,鏈接在底部。

一 安裝

1 解壓,點擊安裝;



2 協(xié)議;



3 選擇目錄;



4 一路next完成。



5 解壓Crack目錄下的內(nèi)容;



6 執(zhí)行上一步解開的東西的安裝,必須不要打開CodeSmith,VS20XX; 安裝后看下開始菜單;如下圖;已經(jīng)裝好;



二 用CodeSmith生成MIS項目的實體層代碼

? ? 實體層代碼,就是對數(shù)據(jù)庫表的映射部分的代碼,如果表多一些的話,手寫是個相當(dāng)大的工作量;

1 ?進(jìn)入CodeSmith,先測試下;在右側(cè)選擇一個自帶示例模板,內(nèi)容如下圖;



2 點擊 Run;生成該CST模板的生成的代碼如下圖;



3 寫生成項目實體層的模板代碼

test.cst:

<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%><%-- 注冊實體層Entity模板 --%> <%@ Register Name="EntityTemplate" Template="D:\WinformTier\Entity.cst" MergeProperties="Flase" ExcludeProperties=""%><%-- 數(shù)據(jù)庫 --%> <%@ Property Name="SourceDatabase" Type="SchemaExplorer.DatabaseSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated."%><% //創(chuàng)建實體層Entity類 this.GenerateEntityClasses();Debug.WriteLine("OK"); %><script runat="template">//生成實體Entity類 private void GenerateEntityClasses(){CodeTemplate Template =new EntityTemplate();foreach(TableSchema table in this.SourceDatabase.Tables){string FileDirectory = OutputDirectory +"\\"+ table.Name +".cs";//生成模板Template.SetProperty("Table",table);//文件輸出Template.RenderToFile(FileDirectory,true);Debug.WriteLine(FileDirectory +" 創(chuàng)建成功.");}} </script><script runat="template">//解決方案輸出路徑 private string Directory = String.Empty;[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor))] [Optional, NotChecked][DefaultValue("")]public string OutputDirectory { get{return Directory;}set{if (value.EndsWith("\\")) value = value.Substring(0, value.Length -1);Directory = value;} } </script>

Entity.cst

<%@ CodeTemplate Inherits="CodeTemplate" Language="C#" TargetLanguage="Text" Description="NetTiers main template." Debug="True" ResponseEncoding="UTF-8"%><%@ Assembly Name="SchemaExplorer"%> <%@ Import Namespace="SchemaExplorer"%><%@ Property Name="Table" Type="TableSchema" DeepLoad="True" Optional="False" Category="01. Getting Started - Required" Description="Database that the tables views, and stored procedures should be based on. IMPORTANT!!! If SourceTables and SourceViews are left blank, the Entire Database will then be generated."%> using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace Entity { publicpartialclass<%= Table.Name%> { <%foreach(ColumnSchema col in Table.Columns){ %> public <%= col.DataType %> <%= col.Name %> { get;set; } <% } %> } }

上述代碼中的“D:\WinformTier\Entity.cst”,可改為自己保存cst的路徑;

另外還要添加數(shù)據(jù)庫連接;添加了之后CodeSmith會自動讀出數(shù)據(jù)庫中的表的名字和字段名,然后生成代碼;

添加數(shù)據(jù)源如下圖,跟VS中類似,此處選擇的是SqlSever架構(gòu)提供者,如果使用別的數(shù)據(jù)庫略有不同;


在右側(cè)屬性面板選中添加的數(shù)據(jù)源和選擇輸出目錄;



然后點擊Run,Build成功;看下輸出目錄;一堆的實體層代碼文件已經(jīng)生成;真的是好多好強大啊;打開一個看一下,沒什么問題,如下圖所示。看來資本主義確實有值得我們學(xué)習(xí)的地方,


codesmith 6.5; 上述模板代碼;Sql Server示例數(shù)據(jù)庫AdventureWorks_Data.mdf;可到俺網(wǎng)盤下載;

codesmith下載:

http://pan.baidu.com/s/1o63dEHW


示例數(shù)據(jù)庫:

http://pan.baidu.com/s/1sjynMGX


模板代碼:

http://pan.baidu.com/s/1o6n4Byy



總結(jié)

以上是生活随笔為你收集整理的图解CodeSmith使用和实用教程一 - 入门和生成MIS项目实体层代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。