.NET Core Mvc和MySql初接触(二)MySql的增删改查

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/yeizt520/article/details/82852847

上一章已经配置好了MySql的连接,这一章就介绍下,EntityFrameWork对Mysql的增删改查操作!
在这里插入图片描述
在之前新建的ApplicationService 建立一个Base接口里面封装一些通用的增删改查的方法:
接口代码为:

 public interface IBaseAppServices<T>
    {
        /// <summary>
        /// 获取实体类
        /// </summary>
        /// <param name="whereLamba">lamba条件表达式</param>
        /// <returns></returns>
        T GetEntity(Expression<Func<T, bool>> whereLamba);
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="whereLamba">lamba条件表达式</param>
        /// <returns></returns>
        int DelEntity(Expression<Func<T, bool>> whereLamba);
        /// <summary>
        /// 保存或修改实体
        /// </summary>
        /// <param name="t"></param>
        /// <param name="type">1保存,2修改</param>
        /// <returns></returns>
        int SaveOrUpdateEntity(T t, int type);
        /// <summary>
        /// 返回列表
        /// </summary>
        /// <typeparam name="S">委托输出的排序实体</typeparam>
        /// <param name="whereLamba">查询条件</param>
        /// <param name="isAsc">是否需要升序排序</param>
        /// <param name="orderLamba">排序条件</param>
        /// <returns></returns>
        IQueryable<T> GetEntitylist<S>(Expression<Func<T, bool>> whereLamba, bool isAsc, Expression<Func<T, S>> orderLamba);

    }

实现代码为:

 public class BaseAppServices<T> : IBaseAppServices<T> where T : class
    {
        public net_mysqlContext db =new net_mysqlContext();//这个是连接mysql时自动生成的dbcontext
        public int DelEntity(Expression<Func<T, bool>> whereLamba)
        {
            try
            {
                T enilty = db.Set<T>().FirstOrDefault(whereLamba);
                db.Set<T>().Attach(enilty);
                db.Entry<T>(enilty).State = EntityState.Deleted;
                return db.SaveChanges();
            }
            catch (Exception e)
            {
                //Logger.Write.Error(e);
                return 0;
            }
        }

        public T GetEntity(Expression<Func<T, bool>> whereLamba)
        {
            try
            {
                T _enilty = db.Set<T>().FirstOrDefault(whereLamba);
                return _enilty;
            }
            catch (Exception e)
            {
                //Logger.Write.Error(e);
                throw;
            }
        }


        /// <summary>
        /// 返回列表
        /// </summary>
        /// <typeparam name="S">委托输出的排序实体</typeparam>
        /// <param name="whereLamba">查询条件</param>
        /// <param name="isAsc">是否需要升序排序</param>
        /// <param name="orderLamba">排序条件</param>
        /// <returns></returns>
        public IQueryable<T> GetEntitylist<S>(Expression<Func<T, bool>> whereLamba, bool isAsc, Expression<Func<T,S>> orderLamba)
        {
            try
            {
                var list = db.Set<T>().Where(whereLamba);
                if (isAsc) list = list.OrderBy<T, S>(orderLamba);
                else list = list.OrderByDescending<T, S>(orderLamba);
                return list;
            }
            catch (Exception e)
            {
                //Logger.Write.Error(e);写日记方法可以先去掉
                return null;
            }
        }
        /// <summary>
        /// 保存或修改实体
        /// </summary>
        /// <param name="t"></param>
        /// <param name="type">1保存,2修改</param>
        /// <returns></returns>
        public int SaveOrUpdateEntity(T t,int type)
        {
            try
            {
                db.Set<T>().Attach(t);
                var res = 0;
                if (type == 1)
                {
                    db.Entry<T>(t).State = EntityState.Added;
                    res = db.SaveChanges();
                }
                if (type == 2)
                {
                    db.Entry<T>(t).State = EntityState.Modified;
                    res = db.SaveChanges();
                }
                return res;
            }
            catch (Exception e)
            {
                //Logger.Write.Error(e);
                return 0;
               
            }
            
        }
    }
}

上面是一些简单的通用封装,里面有调用一些写日志的方法可以先注释掉,下面说下怎么去调用!
在这里插入图片描述
在users文件夹中建立一个接口,和对应的接口的实现方法:

public interface IUserAppService:IBaseAppServices<EnityModel.Users>
    {
        int Login(string userName, string passWord);
    }

这个接口继承IBaseAppServices并且将对应的实体传进去

public class UserAppService : BaseAppServices<EnityModel.Users>,IUserAppService
    {
        net_mysqlContext dbcontext = new net_mysqlContext();

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">des加密密码</param>
        /// <returns></returns>
        public int Login(string userName, string passWord)
        {
            try
            {
                //这里是一个des的加密处理string pass = DesMethod.DESEncrypt(passWord, Constant.DesKey);
                string pass=passWord;
                var user = dbcontext.Users.Where(c => c.UserName == userName && c.Password == pass).FirstOrDefault();
                if (user != null)
                {
                    return user.Id;
                    
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception e)
            {
                //Logger.Write.Info("登录出错:" + e);
                return 0;
            }
        }

    }

上面是IUserAppService的因为接口继承了IBaseAppServices,所以也需要UserAppService实现类也需要继承下BaseAppServices并且将相应的实体类传进去!


好了到这里,到这里关于users实体类的增删改查,以及扩展的登录方法已经配置好了,接下来介绍下怎么引用,.NET Core自带依赖注入,先把这个实现类注册到依赖容器中!

//依赖注入
            services.AddSingleton<ApplicationService.Users.IUserAppService, ApplicationService.Users.UserAppService>();
            services.AddSingleton<ApplicationService.Roles.IRolesAppService, ApplicationService.Roles.RolesAppService>();
            //注入结束

打开Startup.cs在ConfigureServices方法内添加代码
在这里插入图片描述
到这里就配置完成了,在控制器进行构造函数注入直接调用就好了。

       public IUserAppService _iUserAppService;
       
        public LoginController(IUserAppService iuserAppService)
        {
            _iUserAppService = iuserAppService;

        }
        

依赖注入完成后就可以直接调用使用了,例如:var user= _iUserAppService.GetEntity(c => c.Id == id);

展开阅读全文

没有更多推荐了,返回首页