C#實現(xiàn)根據(jù)實體類自動創(chuàng)建數(shù)據(jù)庫表
.Net新手通常容易把屬性(Property)跟特性(Attribute)搞混,其實這是兩種不同的東西
屬性指的類中封裝的數(shù)據(jù)字段;而特性是對類、字段、方法和屬性等元素標(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)在需要通過定義一些實體類,動態(tài)創(chuàng)建出對應(yīng)的數(shù)據(jù)庫表,該怎么做呢?
直接上代碼
namespace CustomerAttribute
{
/// <summary>
/// 數(shù)據(jù)庫主鍵
/// </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ù)庫主鍵字段
/// </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類,我們可以對類、方法、屬性等元素進(jìn)行特性標(biāo)注
上邊是一個簡單示例,我們可以通過自定義[DbKey]特性,標(biāo)注在需要設(shè)置主鍵的字段上
需要動態(tài)創(chuàng)建數(shù)據(jù)庫的時候,可以從實體類中解析出表名、字段名、主鍵字段、字段說明等等,然后生成創(chuàng)建數(shù)據(jù)庫表的腳本,動態(tài)創(chuàng)建數(shù)據(jù)庫表
創(chuàng)建數(shù)據(jù)庫的代碼,后邊可以進(jìn)一步補(bǔ)充
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word
眾所周知,PDF 文檔支持特長文件,集成度和安全可靠性都較高,可有效防止他人對 PDF 內(nèi)容進(jìn)行更改,所以在工作中深受大家喜愛。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下2022-12-12
C#使用ThreadPriority設(shè)置線程優(yōu)先級
這篇文章介紹了C#使用ThreadPriority設(shè)置線程優(yōu)先級的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
Unity?UGUI的PointerEventData的介紹及使用
這篇文章主要為大家介紹了Unity?UGUI的PointerEventData的介紹及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C#使用JavaScriptSerializer序列化時的時間類型處理
這篇文章主要為大家詳細(xì)介紹了C#使用JavaScriptSerializer序列化時的時間類型處理,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
C#實現(xiàn)通過winmm.dll控制聲音播放的方法
這篇文章主要介紹了C#實現(xiàn)通過winmm.dll控制聲音播放的方法,很實用的功能,需要的朋友可以參考下2014-08-08

