十年河东,十年河西,莫欺少年穷

学无止境,精益求精

我搭建项目的习惯,一般先搭建项目整体的层次划分,首先贴出我搭建项目的各个层次划分:

netCore3.1项目搭建过程记录-省的每次都傻乎乎的不知道应该先干啥

 

 

 项目分为8层,数据库层采用EfCore 结合 sqlSugar的方式,从上到下依次为:公共类层、EfCore上下文层、SqlSugar上下文层,业务实体层、sqlSugar实体层【由工具生成】、接口层、服务层、web站点【webApi属于这一层】,各层次之间的引用,我就不累述了。

清晰了项目的各层次,下面我们逐步完善这个裸体项目

1、搭建你的数据库上下文层

1.1、搭建EfCore上下文

1、打开程序包管理控制台,选中EFCoreContext层,并依次执行如下控制命令

 Install-Package Microsoft.EntityFrameworkCore
 Install-Package Microsoft.EntityFrameworkCore.SqlServer
 Install-Package Microsoft.EntityFrameworkCore.Tools
 Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 3.1.4

2、选中启动项目webSite ,并依次执行上述控制命令

 Install-Package Microsoft.EntityFrameworkCore
 Install-Package Microsoft.EntityFrameworkCore.SqlServer
 Install-Package Microsoft.EntityFrameworkCore.Tools
 Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 3.1.4

如下图:

netCore3.1项目搭建过程记录-省的每次都傻乎乎的不知道应该先干啥

 

 

 3、以上两步骤执行完毕后,在程序包管理控制台中继续切换到EFCoreContext层,并执行如下指令生成数据库上下文【注意,连接字符串要改成你自己的】

netCore3.1项目搭建过程记录-省的每次都傻乎乎的不知道应该先干啥

 

 

4、执行成功后,生成的上下文如下:

netCore3.1项目搭建过程记录-省的每次都傻乎乎的不知道应该先干啥

 

 

 我的数据库脚本请参考:通用权限管理【数据库】设计方案

最后在启动类中注册SqlServer服务,如下:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            #region 注册SQLSERVER
            services.AddDbContext<DbRoleManagerContext>(options =>
                 options.UseSqlServer(Configuration.GetConnectionString("WuAnDBContext")));
            #endregion

这样的话,EFCore的上下文就生成了,因为netCore对原生SQL的支持不是特别好,因此,我的项目中引入了sqlSugar作为支持。下面我们来搭建sqlSugar的上下文。

1.2、搭建sqlSugar上下文及sqlsugar实体

没有玩过SqlSugar的小虎斑可以参考这篇博客:SqlSugar+SqlServer+NetCore3.1 入门案例

SqlSugar的工具【可通过项目/工具生成】大家可参考:http://www.codeisbug.com/Doc/8/1123 或者 直接去CSDN 上下载相关工具/项目:https://download.csdn.net/download/wolongbb/12997789

1、在项目SqlSugarContext、SqlSugarModel 中,通过NuGet安装sqlSugar引用 和 SqlSugar的依赖项Newtonsoft.Json V 12.0.3 及 System.Data.SqlClient V 4.8.2 版本

netCore3.1项目搭建过程记录-省的每次都傻乎乎的不知道应该先干啥

 

 

 2、通过下载的项目,生成sqlSugar上下文及sqlsugar实体

注意:在下载的项目SoEasyPlatform-master中需要自行修改连接字符串及命名空间

netCore3.1项目搭建过程记录-省的每次都傻乎乎的不知道应该先干啥

我的sqlsugar上下文如下:

using SqlSugarModel.Enties;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SugarContext
{
    public class SugarDbContext
    {
        /// 获取连接字符串        
        private static string Connection = "Data Source=LAPTOP-P5GVS4UM;Initial Catalog=DbRoleManager;User ID=sa;Password=Aa123456";

        public SugarDbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = Connection,
                DbType = DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };

        }
        //注意:不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
    }
    public class SugarDbContext<T> where T : class, new()
    {
        public SugarDbContext()
        {
            Db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Data Source=LAPTOP-P5GVS4UM;Initial Catalog=DbRoleManager;User ID=sa;Password=Aa123456",
                DbType = DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了

            });
            //调式代码 用来打印SQL 
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                    Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };

        }
        //注意:不能写成静态的
        public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
        public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }//用来操作当前表的数据

        public SimpleClient<User_Dept> User_DeptDb { get { return new SimpleClient<User_Dept>(Db); } }//用来处理User_Dept表的常用操作
        public SimpleClient<User_MenuButton> User_MenuButtonDb { get { return new SimpleClient<User_MenuButton>(Db); } }//用来处理User_MenuButton表的常用操作
        public SimpleClient<User_Role> User_RoleDb { get { return new SimpleClient<User_Role>(Db); } }//用来处理User_Role表的常用操作
        public SimpleClient<User_Role_Dept> User_Role_DeptDb { get { return new SimpleClient<User_Role_Dept>(Db); } }//用来处理User_Role_Dept表的常用操作
        public SimpleClient<User_Role_MenuButton> User_Role_MenuButtonDb { get { return new SimpleClient<User_Role_MenuButton>(Db); } }//用来处理User_Role_MenuButton表的常用操作
        public SimpleClient<User_Account> User_AccountDb { get { return new SimpleClient<User_Account>(Db); } }//用来处理User_Account表的常用操作
        public SimpleClient<User_Account_Dept> User_Account_DeptDb { get { return new SimpleClient<User_Account_Dept>(Db); } }//用来处理User_Account_Dept表的常用操作
        public SimpleClient<User_Account_Role> User_Account_RoleDb { get { return new SimpleClient<User_Account_Role>(Db); } }//用来处理User_Account_Role表的常用操作


        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetList()
        {
            return CurrentDb.GetList();
        }

        /// <summary>
        /// 根据表达式查询
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetList(Expression<Func<T, bool>> whereExpression)
        {
            return CurrentDb.GetList(whereExpression);
        }


        /// <summary>
        /// 根据表达式查询分页
        /// </summary>
        /// <returns></returns>
        public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel)
        {
            return CurrentDb.GetPageList(whereExpression, pageModel);
        }

        /// <summary>
        /// 根据表达式查询分页并排序
        /// </summary>
        /// <param name="whereExpression">it</param>
        /// <param name="pageModel"></param>
        /// <param name="orderByExpression">it=>it.id或者it=>new{it.id,it.name}</param>
        /// <param name="orderByType">OrderByType.Desc</param>
        /// <returns></returns>
        public virtual List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel pageModel, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        {
            return CurrentDb.GetPageList(whereExpression, pageModel, orderByExpression, orderByType);
        }


        /// <summary>
        /// 根据主键查询
        /// </summary>
        /// <returns></returns>
        public virtual T GetById(dynamic id)
        {
            return CurrentDb.GetById(id);
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(dynamic id)
        {
            return CurrentDb.Delete(id);
        }


        /// <summary>
        /// 根据实体删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(T data)
        {
            return CurrentDb.Delete(data);
        }

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(dynamic[] ids)
        {
            return CurrentDb.AsDeleteable().In(ids).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据表达式删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Delete(Expression<Func<T, bool>> whereExpression)
        {
            return CurrentDb.Delete(whereExpression);
        }


        /// <summary>
        /// 根据实体更新,实体需要有主键
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Update(T obj)
        {
            return CurrentDb.Update(obj);
        }

        /// <summary>
        ///批量更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Update(List<T> objs)
        {
            return CurrentDb.UpdateRange(objs);
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Insert(T obj)
        {
            return CurrentDb.Insert(obj);
        }


        /// <summary>
        /// 批量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual bool Insert(List<T> objs)
        {
            return CurrentDb.InsertRange(objs);
        }


        //自已扩展更多方法 
    }
}
View Code

相关文章:

  • 2021-06-28
  • 2021-07-13
  • 2021-11-30
  • 2022-12-23
  • 2021-06-06
  • 2021-10-26
  • 2021-08-02
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2021-09-07
  • 2021-12-08
  • 2021-12-12
  • 2021-06-22
  • 2021-11-04
相关资源
相似解决方案