@ 簡介 什麼是Dikeko.ORM? Dikeko.ORM是一個簡單的.NET輕量級的ORM,目前僅支持SqlServer資料庫。 安裝 .NET版:https://www.nuget.org/packages/Dikeko.ORM PM>Install-Package Dikeko.ORM .N ...
@目錄
簡介
什麼是Dikeko.ORM?
Dikeko.ORM是一個簡單的.NET輕量級的ORM,目前僅支持SqlServer資料庫。
安裝
.NET版:https://www.nuget.org/packages/Dikeko.ORM
PM>Install-Package Dikeko.ORM
.NET CORE版:https://www.nuget.org/packages/Dikeko.ORM.Core
PM>Install-Package Dikeko.ORM.Core
源碼
.NET 版:https://github.com/cnsmallant/Dikeko.ORM
.NET CORE版:https://github.com/cnsmallant/Dikeko.ORM.Core
註意
Dikeko.ORM僅支持SqlServer資料庫,.NET CORE版需要手動添加實體類,或者下載源碼獲取T4模版。
如果遇到不明白的或者發現BUG請加入QQ群:Java .Net Go PHP UI群:574879752 直接@群主
實體類示例:
[TableName("UserInfo")]
public class UserInfo
{
/// <summary>
///主鍵
/// </summary>
[Column("UserId")]
[PrimaryKey("UserId", false)]
public string UserId { get; set; }
/// <summary>
///UserCode
/// </summary>
[Column("UserCode")]
public string UserCode { get; set; }
/// <summary>
///昵稱
/// </summary>
[Column("UserNickName")]
public string UserNickName { get; set; }
/// <summary>
///手機號
/// </summary>
[Column("UserPhone")]
public string UserPhone { get; set; }
/// <summary>
///密碼
/// </summary>
[Column("UserPassWord")]
public string UserPassWord { get; set; }
/// <summary>
///微信OPENID
/// </summary>
[Column("WxOpenId")]
public string WxOpenId { get; set; }
/// <summary>
///QQOPENID
/// </summary>
[Column("QqOpenId")]
public string QqOpenId { get; set; }
/// <summary>
///支付寶OPENID
/// </summary>
[Column("AlipayOpenId")]
public string AlipayOpenId { get; set; }
/// <summary>
///新浪OPENID
/// </summary>
[Column("SianOpenId")]
public string SianOpenId { get; set; }
/// <summary>
///狀態
/// </summary>
[Column("Status")]
public int Status { get; set; }
/// <summary>
///創建人
/// </summary>
[Column("CreateUserId")]
public string CreateUserId { get; set; }
/// <summary>
///創建時間
/// </summary>
[Column("CreateTime")]
public DateTime CreateTime { get; set; }
/// <summary>
///更新人
/// </summary>
[Column("UpdateUserId")]
public string UpdateUserId { get; set; }
/// <summary>
///更新時間
/// </summary>
[Column("UpdateTime")]
public DateTime UpdateTime { get; set; }
/// <summary>
///排序
/// </summary>
[Column("Sort")]
public int Sort { get; set; }
}
方法
public interface IDikekoDataBase
{
#region 資料庫操作方法
/// <summary>
/// 執行插入
/// </summary>
/// <typeparam name="T">實體類</typeparam>
/// <param name="t">實體參數</param>
/// <returns></returns>
dynamic Insert<T>(T t);
/// <summary>
/// 執行插入
/// </summary>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
dynamic Insert(string sql, params object[] args);
/// <summary>
/// 批量添加(最多一次性1000條數據)
/// </summary>
/// <typeparam name="T">實體</typeparam>
/// <param name="t">實體參數</param>
/// <param name="valuesArray">插入值組合,例如('a1','11','你好1'),('a2','12','你好2'),('a3','13','你好3'),('a4','14','你好4'),('a5','15','你好5')</param>
/// <returns></returns>
dynamic BulkInsert<T>(T t, string valuesArray);
/// <summary>
/// 批量插入(最多一次性1000條數據)
/// </summary>
/// <param name="table">表名</param>
/// <param name="field">欄位</param>
/// <param name="valuesArray">插入值組合,例如('a1','11','你好1'),('a2','12','你好2'),('a3','13','你好3'),('a4','14','你好4'),('a5','15','你好5')</param>
/// <returns></returns>
dynamic BulkInsert(string table, string field, string valuesArray);
/// <summary>
/// 執行修改
/// </summary>
/// <typeparam name="T">實體類</typeparam>
/// <param name="t">實體參數</param>
/// <returns></returns>
dynamic Update<T>(T t);
/// <summary>
/// 執行修改
/// </summary>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
dynamic Update(string sql, params object[] args);
/// <summary>
/// 執行刪除
/// </summary>
/// <typeparam name="T">實體類</typeparam>
/// <param name="t">實體參數</param>
/// <returns></returns>
dynamic Delete<T>(T t);
/// <summary>
/// 執行刪除
/// </summary>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
dynamic Delete(string sql, params object[] args);
/// <summary>
/// 預設第一條
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T FirstOrDefault<T>();
/// <summary>
/// 預設第一條
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
T FirstOrDefault<T>(string sql, params object[] args);
/// <summary>
/// 預設一條
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
T SingleOrDefault<T>(T t);
/// <summary>
/// 查詢一條數據
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
T SingleOrDefault<T>(string sql, params object[] args);
/// <summary>
/// 查詢總數
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
object CountOrDefault<T>();
/// <summary>
/// 查詢總數
/// </summary>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
object CountOrDefault(string sql, params object[] args);
/// <summary>
/// 查詢總數
/// </summary>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
object CountOrDefault<T>(string sql, params object[] args);
/// <summary>
/// 查詢
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
List<T> FetchOrDefault<T>();
/// <summary>
/// 查詢
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql"></param>
/// <param name="args"></param>
/// <returns></returns>
List<T> FetchOrDefault<T>(string sql, params object[] args);
/// <summary>
/// 分頁
/// </summary>
/// <typeparam name="T">實體</typeparam>
/// <param name="CurrentPage">當前頁</param>
/// <param name="PageSize">每頁條數</param>
/// <param name="sql">sql語句</param>
/// <param name="args">參數</param>
/// <returns></returns>
Page<T> PageOrDefault<T>(int CurrentPage, int PageSize, string sql, params object[] args);
/// <summary>
/// 事務
/// </summary>
/// <param name="sql">sql語句</param>
/// <param name="args">參數</param>
/// <returns></returns>
dynamic Transaction(string sql, params object[] args);
#endregion
}
使用示例
public class UserInfo
{
/// <summary>
/// 當前選擇
/// </summary>
public static UserInfo Current
{
get
{
return new UserInfo();
}
}
/// <summary>
/// 實例化資料庫操作方法
/// </summary>
DikekoDataBase db = new DikekoDataBase("DefaultConnection");
/// <summary>
/// 添加用戶
/// </summary>
/// <returns></returns>
public int UserAdd()
{
try
{
var guid = Guid.NewGuid().ToString("N");
Model.UserInfo user = new Model.UserInfo
{
UserId = guid,
UserCode = "Dikeko1234566",
UserNickName = "Dikeko111",
UserPhone = "13212345678",
UserPassWord = "123456",
WxOpenId = string.Empty,
QqOpenId = string.Empty,
AlipayOpenId = string.Empty,
SianOpenId = string.Empty,
Status = 0,
CreateUserId = guid,
CreateTime = DateTime.Now,
UpdateUserId = guid,
UpdateTime = DateTime.Now,
Sort = 0
};
var result = db.Insert(user);
return result;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// sql語句添加用戶
/// </summary>
/// <returns></returns>
public int UserAddForSql()
{
try
{
string sql = @"INSERT INTO UserInfo
(
UserId
,UserCode
,UserNickName
)
VALUES
(
@UserId
,@UserCode
,@UserNickName
)";
var guid = Guid.NewGuid().ToString("N");
Model.UserInfo user = new Model.UserInfo
{
UserId = guid,
UserCode = "Dikeko123466",
UserNickName = "Dikeko66",
};
object[] args =
{
new SqlParameter("@UserId",user.UserId),
new SqlParameter("@UserCode",user.UserCode),
new SqlParameter("@UserNickName",user.UserNickName),
};
var result = db.Insert(sql, args);
return result;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 用戶修改
/// </summary>
/// <returns></returns>
public int UserUpdate()
{
Model.UserInfo user = new Model.UserInfo
{
UserId = "ce4bf2d6874f4be0adcf5d7fef12eabf",
UserCode = "Dikeko123456",
UserNickName = "Dikeko001",
UserPhone = "13212345678",
UserPassWord = "123456",
WxOpenId = string.Empty,
QqOpenId = string.Empty,
AlipayOpenId = string.Empty,
SianOpenId = string.Empty,
Status = 0,
CreateUserId = "ce4bf2d6874f4be0adcf5d7fef12eabf",
CreateTime = DateTime.Now,
UpdateUserId = "ce4bf2d6874f4be0adcf5d7fef12eabf",
UpdateTime = DateTime.Now,
Sort = 0
};
var result = db.Update(user);
return result;
}
/// <summary>
/// 讀取用戶
/// </summary>
/// <returns></returns>
public dynamic GetUser()
{
var list = db.FetchOrDefault<Model.UserInfo>();
return JsonConvert.SerializeObject(list);//轉換成JSON返回
}
/// <summary>
/// 讀取用戶
/// </summary>
/// <returns></returns>
public dynamic GetUserById()
{
Model.UserInfo user = new Model.UserInfo
{
UserId = "ce4bf2d6874f4be0adcf5d7fef12eabf",
};
var info = db.SingleOrDefault(user);
return JsonConvert.SerializeObject(info);//轉換成JSON返回
}
/// <summary>
/// 分頁讀取
/// </summary>
/// <returns></returns>
public dynamic GetUserPage()
{
string sql = "SELECT * FROM UserInfo";
var list = db.PageOrDefault<Model.UserInfo>(1, 10, sql);
return JsonConvert.SerializeObject(list);//轉換成JSON返回
}
}