mvc学习

  • 判断记住密码的时候用的是cookie还是session
 if ( userManager.Login(model.Email, model.LoginPwd,out  userid))
                {
                    //跳转
                    //记住密码的时候判断是用session还是用cookie
                    if (model.RememberMe)
                    {
                        Response.Cookies.Add(new HttpCookie("loginName")
                        {
                            Value = model.Email,
                            Expires = DateTime.Now.AddDays(7)
                        });
                        Response.Cookies.Add(new HttpCookie("userId")
                        {
                            Value = userid.ToString(),
                            Expires = DateTime.Now.AddDays(7)
                        });
                    }
                    else
                    {
                        Session["loginName"] = model.Email;
                        Session["userId"] = userid;
                    }
  • DbContext
 /// 
        /// 上下文
        /// 
        private readonly BlogContext _db;
        public BaseService(Models.BlogContext db)
        {
            this._db = db;
        }
  • CreateAsync:增
public async Task CreateAsync(T model, bool saved = true)
        {
            _db.Set().Add(model);
            if (saved) await _db.SaveChangesAsync();
        }
  • EditAsync :改
/// 
        /// _db.Configuration.ValidateOnSaveEnabled = false;关闭ef的自动检查
        /// _db.Entry(model).State = EntityState.Modified;把他的状态改成可修改的
        ///  if (saved)
        ///{
        ///      await _db.SaveChangesAsync();
        ///      _db.Configuration.ValidateOnSaveEnabled = true;
        ///}
        ///修改,并且保存
        /// 
        /// 
        /// 
        /// 
        public async Task EditAsync(T model, bool saved = true)
        {
            _db.Configuration.ValidateOnSaveEnabled = false;
            _db.Entry(model).State = EntityState.Modified;
            if (saved)
            {
                await _db.SaveChangesAsync();
                _db.Configuration.ValidateOnSaveEnabled = true;
            }

        }
  • Get :查
/// 
        /// return _db.Set().Where(m => !m.IsRemoved).AsNoTracking();这是返回所有的未被删除的数据(没有真的执行)
        /// 
        /// 
        public IQueryable GetAllAsync()
        {
            return _db.Set().Where(m => !m.IsRemoved).AsNoTracking();
        }

        /// 
        /// return GetAllAsync().Skip(pageSize * pageIndex).Take(pageSize);刨除了pageSize * pageIndex个数据,剩余数据取前pageSize个
        /// 
        /// 
        /// 
        /// 
        public IQueryable GetAllByPageAsync(int pageSize = 10, int pageIndex = 0)
        {
            return GetAllAsync().Skip(pageSize * pageIndex).Take(pageSize);
        }

        public IQueryable GetAllByPageOrderAsync(int pageSize = 10, int pageIndex = 0, bool asc = true)
        {
            return GetAllOrderAsync(asc).Skip(pageSize * pageIndex).Take(pageSize);
        }


        public IQueryable GetAllOrderAsync(bool asc = true)
        {
            var datas = GetAllAsync();
            if (asc)
            {
                datas = datas.OrderBy(m => m.CreateTime);
            }

            else
            {
                datas = datas.OrderByDescending(m => m.CreateTime);
            }

            return datas;
        }

        /// 
        /// 得到数据一定是异步的
        /// 
        /// 
        /// 
        public async Task GetOneAsyncAsync(Guid id)
        {
            return await GetAllAsync().FirstAsync(m => m.Id == id);
        }

        public async Task RemoveAsync(Guid id, bool saved = true)
        {
            _db.Configuration.ValidateOnSaveEnabled = false;
            var t = new T() { Id = id };
            _db.Entry(t).State = EntityState.Unchanged;
            t.IsRemoved = true;
            if (saved)
            {
                await _db.SaveChangesAsync();
                _db.Configuration.ValidateOnSaveEnabled = true;
            }
        }
  • RemoveAsync : 删
 public async Task RemoveAsync(T model, bool saved = true)
        {
            await RemoveAsync(model.Id, saved);
        }

-Save: 存

 public async Task Save()
        {
            await _db.SaveChangesAsync();
            _db.Configuration.ValidateOnSaveEnabled = true;
        }

你可能感兴趣的:(mvc学习)