- 判断记住密码的时候用的是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
/// <summary>
/// 上下文
/// </summary>
private readonly BlogContext _db;
public BaseService(Models.BlogContext db)
{
this._db = db;
}
- CreateAsync:增
public async Task CreateAsync(T model, bool saved = true)
{
_db.Set<T>().Add(model);
if (saved) await _db.SaveChangesAsync();
}
- EditAsync :改
/// <summary>
/// _db.Configuration.ValidateOnSaveEnabled = false;关闭ef的自动检查
/// _db.Entry(model).State = EntityState.Modified;把他的状态改成可修改的
/// if (saved)
///{
/// await _db.SaveChangesAsync();
/// _db.Configuration.ValidateOnSaveEnabled = true;
///}
///修改,并且保存
/// </summary>
/// <param name="model"></param>
/// <param name="saved"></param>
/// <returns></returns>
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 :查
/// <summary>
/// return _db.Set<T>().Where(m => !m.IsRemoved).AsNoTracking();这是返回所有的未被删除的数据(没有真的执行)
/// </summary>
/// <returns></returns>
public IQueryable<T> GetAllAsync()
{
return _db.Set<T>().Where(m => !m.IsRemoved).AsNoTracking();
}
/// <summary>
/// return GetAllAsync().Skip(pageSize * pageIndex).Take(pageSize);刨除了pageSize * pageIndex个数据,剩余数据取前pageSize个
/// </summary>
/// <param name="pageSize"></param>
/// <param name="pageIndex"></param>
/// <returns></returns>
public IQueryable<T> GetAllByPageAsync(int pageSize = 10, int pageIndex = 0)
{
return GetAllAsync().Skip(pageSize * pageIndex).Take(pageSize);
}
public IQueryable<T> GetAllByPageOrderAsync(int pageSize = 10, int pageIndex = 0, bool asc = true)
{
return GetAllOrderAsync(asc).Skip(pageSize * pageIndex).Take(pageSize);
}
public IQueryable<T> GetAllOrderAsync(bool asc = true)
{
var datas = GetAllAsync();
if (asc)
{
datas = datas.OrderBy(m => m.CreateTime);
}
else
{
datas = datas.OrderByDescending(m => m.CreateTime);
}
return datas;
}
/// <summary>
/// 得到数据一定是异步的
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<T> 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;
}