C#實(shí)現(xiàn)根據(jù)實(shí)體類自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)表
.Net新手通常容易把屬性(Property)跟特性(Attribute)搞混,其實(shí)這是兩種不同的東西
屬性指的類中封裝的數(shù)據(jù)字段;而特性是對(duì)類、字段、方法和屬性等元素標(biāo)注的聲明性信息
如下代碼(Id、Name為User的屬性,[DbKey]為Id的特性)
/// <summary> /// 用戶信息 /// </summary> public class User { [DbKey] public string Id { get; set; } public string Name { get; set; } }
特性分預(yù)定義特性和自定義特性,本節(jié)主要講述自定義特性
特性能解決什么問題?
假如現(xiàn)在需要通過定義一些實(shí)體類,動(dòng)態(tài)創(chuàng)建出對(duì)應(yīng)的數(shù)據(jù)庫(kù)表,該怎么做呢?
直接上代碼
namespace CustomerAttribute { /// <summary> /// 數(shù)據(jù)庫(kù)主鍵 /// </summary> public class DbKey : Attribute { public string Description { get; set; } public DbKey() { } public DbKey(string description) { this.Description = description; } } }
namespace CustomerAttribute { /// <summary> /// 用戶信息 /// </summary> public class User { [DbKey] public string Id { get; set; } public string Name { get; set; } } /// <summary> /// 用戶角色 /// </summary> public class UserRole { [DbKey("用戶ID")] public string UserId { get; set; } [DbKey("角色I(xiàn)D")] public string RoleId { get; set; } } }
namespace CustomerAttribute { class Program { /// <summary> /// 獲取數(shù)據(jù)庫(kù)主鍵字段 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> private static IEnumerable<PropertyInfo> getDbKeyFields<T>() { // 獲取當(dāng)前類中的公共字段 var fields = typeof(T).GetProperties(); // 查找有DbKey特性的字段 var keyFields = fields.Where(field => (DbKey)Attribute.GetCustomAttribute(field, typeof(DbKey)) != null); return keyFields; } private static string getDescription(PropertyInfo field) { string result = string.Empty; var dbKey = (DbKey)Attribute.GetCustomAttribute(field, typeof(DbKey)); if (dbKey != null) result = dbKey.Description; return result; } static void Main(string[] args) { try { var userKeyFields = getDbKeyFields<User>(); Console.WriteLine("User表的主鍵為:" + string.Join(",", userKeyFields.Select(field => field.Name))); var userRoleKeyFields = getDbKeyFields<UserRole>(); Console.WriteLine("UserRole表的主鍵為:" + string.Join(",", userRoleKeyFields.Select(field => field.Name))); foreach (PropertyInfo field in userRoleKeyFields) { string description = getDescription(field); Console.WriteLine(string.Format("{0}字段的描述信息為:{1}", field.Name, description)); } } catch (Exception ex) { Console.WriteLine(ex); } finally { Console.ReadLine(); } } } }
從上邊代碼可以看出來,特性本身也是類,繼承自Attribute類,我們可以對(duì)類、方法、屬性等元素進(jìn)行特性標(biāo)注
上邊是一個(gè)簡(jiǎn)單示例,我們可以通過自定義[DbKey]特性,標(biāo)注在需要設(shè)置主鍵的字段上
需要?jiǎng)討B(tài)創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候,可以從實(shí)體類中解析出表名、字段名、主鍵字段、字段說明等等,然后生成創(chuàng)建數(shù)據(jù)庫(kù)表的腳本,動(dòng)態(tài)創(chuàng)建數(shù)據(jù)庫(kù)表
創(chuàng)建數(shù)據(jù)庫(kù)的代碼,后邊可以進(jìn)一步補(bǔ)充
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持我們!
上一篇:C#使用UdpClient類進(jìn)行簡(jiǎn)單通信的實(shí)例
欄 目:C#教程
本文標(biāo)題:C#實(shí)現(xiàn)根據(jù)實(shí)體類自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)表
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6085.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法