一个简单的.NET轻量级的ORM——Dikeko.ORM

@

目录
  • 简介
    • 什么是Dikeko.ORM?
    • 安装
    • 源码
    • 注意
  • 方法
  • 使用示例

简介

什么是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
    {
        /// 
        ///主键
        /// 
        [Column("UserId")]
        [PrimaryKey("UserId", false)]
        public string UserId { get; set; }
        /// 
        ///UserCode
        /// 
        [Column("UserCode")]
        public string UserCode { get; set; }
        /// 
        ///昵称
        /// 
        [Column("UserNickName")]
        public string UserNickName { get; set; }
        /// 
        ///手机号
        /// 
        [Column("UserPhone")]
        public string UserPhone { get; set; }
        /// 
        ///密码
        /// 
        [Column("UserPassWord")]
        public string UserPassWord { get; set; }
        /// 
        ///微信OPENID
        /// 
        [Column("WxOpenId")]
        public string WxOpenId { get; set; }
        /// 
        ///QQOPENID
        /// 
        [Column("QqOpenId")]
        public string QqOpenId { get; set; }
        /// 
        ///支付宝OPENID
        /// 
        [Column("AlipayOpenId")]
        public string AlipayOpenId { get; set; }
        /// 
        ///新浪OPENID
        /// 
        [Column("SianOpenId")]
        public string SianOpenId { get; set; }
        /// 
        ///状态
        /// 
        [Column("Status")]
        public int Status { get; set; }
        /// 
        ///创建人
        /// 
        [Column("CreateUserId")]
        public string CreateUserId { get; set; }
        /// 
        ///创建时间
        /// 
        [Column("CreateTime")]
        public DateTime CreateTime { get; set; }
        /// 
        ///更新人
        /// 
        [Column("UpdateUserId")]
        public string UpdateUserId { get; set; }
        /// 
        ///更新时间
        /// 
        [Column("UpdateTime")]
        public DateTime UpdateTime { get; set; }
        /// 
        ///排序
        /// 
        [Column("Sort")]
        public int Sort { get; set; }
    }

方法

    public interface IDikekoDataBase
    {
        #region 数据库操作方法
        /// 
        /// 执行插入
        /// 
        /// 实体类
        /// 实体参数
        /// 
        dynamic Insert(T t);


        /// 
        /// 执行插入
        /// 
        /// 
        /// 
        /// 
        dynamic Insert(string sql, params object[] args);


        /// 
        /// 批量添加(最多一次性1000条数据)
        /// 
        /// 实体
        /// 实体参数
        /// 插入值组合,例如('a1','11','你好1'),('a2','12','你好2'),('a3','13','你好3'),('a4','14','你好4'),('a5','15','你好5')
        /// 
        dynamic BulkInsert(T t, string valuesArray);



        /// 
        /// 批量插入(最多一次性1000条数据)
        /// 
        /// 表名
        /// 字段
        /// 插入值组合,例如('a1','11','你好1'),('a2','12','你好2'),('a3','13','你好3'),('a4','14','你好4'),('a5','15','你好5')
        /// 
        dynamic BulkInsert(string table, string field, string valuesArray);


        /// 
        /// 执行修改
        /// 
        /// 实体类
        /// 实体参数
        /// 
        dynamic Update(T t);

        /// 
        /// 执行修改
        /// 
        /// 
        /// 
        /// 
        dynamic Update(string sql, params object[] args);


        /// 
        /// 执行删除
        /// 
        /// 实体类
        /// 实体参数
        /// 
        dynamic Delete(T t);

        /// 
        /// 执行删除
        /// 
        /// 
        /// 
        /// 
        dynamic Delete(string sql, params object[] args);


        /// 
        /// 默认第一条
        /// 
        /// 
        /// 
        T FirstOrDefault();


        /// 
        /// 默认第一条
        /// 
        /// 
        /// 
        /// 
        /// 
        T FirstOrDefault(string sql, params object[] args);


        /// 
        /// 默认一条
        /// 
        /// 
        /// 
        T SingleOrDefault(T t);


        /// 
        /// 查询一条数据
        /// 
        /// 
        /// 
        /// 
        /// 
        T SingleOrDefault(string sql, params object[] args);

        /// 
        /// 查询总数
        /// 
        /// 
        /// 
        object CountOrDefault();


        /// 
        /// 查询总数
        /// 
        /// 
        /// 
        /// 
        object CountOrDefault(string sql, params object[] args);


        /// 
        /// 查询总数
        /// 
        /// 
        /// 
        /// 
        object CountOrDefault(string sql, params object[] args);

        /// 
        /// 查询
        /// 
        /// 
        /// 
        List FetchOrDefault();


        /// 
        /// 查询
        /// 
        /// 
        /// 
        /// 
        /// 
        List FetchOrDefault(string sql, params object[] args);


        /// 
        /// 分页
        /// 
        /// 实体
        /// 当前页
        /// 每页条数
        /// sql语句
        /// 参数
        /// 
        Page PageOrDefault(int CurrentPage, int PageSize, string sql, params object[] args);


        /// 
        /// 事务
        /// 
        /// sql语句
        /// 参数
        /// 
        dynamic Transaction(string sql, params object[] args);
      

        #endregion

    }

使用示例

public class UserInfo
{

    /// 
    /// 当前选择
    /// 
    public static UserInfo Current
    {
        get
        {
            return new UserInfo();
        }
    }

    /// 
    /// 实例化数据库操作方法
    /// 
    DikekoDataBase db = new DikekoDataBase("DefaultConnection");
    /// 
    /// 添加用户
    /// 
    /// 
    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;
        }
    }

    /// 
    /// sql语句添加用户
    /// 
    /// 
    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;
        }
    }

    /// 
    /// 用户修改
    /// 
    /// 
    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;
    }
    /// 
    /// 读取用户
    /// 
    /// 
    public dynamic GetUser()
    {
        var list = db.FetchOrDefault();
        return JsonConvert.SerializeObject(list);//转换成JSON返回
    }

    /// 
    /// 读取用户
    /// 
    /// 
    public dynamic GetUserById()
    {
        Model.UserInfo user = new Model.UserInfo
        {
            UserId = "ce4bf2d6874f4be0adcf5d7fef12eabf",
        };
        var info = db.SingleOrDefault(user);
        return JsonConvert.SerializeObject(info);//转换成JSON返回
    }
    /// 
    /// 分页读取
    /// 
    /// 
    public dynamic GetUserPage()
    {
        string sql = "SELECT * FROM UserInfo";
        var list = db.PageOrDefault(1, 10, sql);
        return JsonConvert.SerializeObject(list);//转换成JSON返回
    }
}                                      

你可能感兴趣的:(一个简单的.NET轻量级的ORM——Dikeko.ORM)