EF中登录的方法
public Users Login(string name,string pwd) { Users list = db.Users.Where(p=>p.UName==name && p.Pwd==pwd).FirstOrDefault(); LogHelper.TextWriter("执行登录操作"); return list; }
修改
public int Upt(T t) { db.Set<T>().Attach(t); db.Entry<T>(t).State = EntityState.Modified; return db.SaveChanges(); }
单删批删操作
public int Del(int id) { var obj = db.Set<T>().Find(id); db.Set<T>().Remove(obj); return db.SaveChanges(); } public int Dels(string ids,string TName) { string sql = string.Format("delete from {0} where Id in ({1})", TName, ids); return db.Database.ExecuteSqlCommand(sql); }
public int TurnEmployee(int id)
{
var obj = All.Show().Find(id);
obj.Z = DateTime.Now.ToString("yyyy-MM-dd");
obj.L = "\\";
obj.ZT = 1;
return All.Upt(obj);
}
EF框架中存储存储过程分页的调用
//添加 [HttpPost] [Route("api/Insert")] public int Insert(DayWord word) { int flag = 0; using (var ef = new MyContext()) { word.Dtime = DateTime.Now.ToString("yyyy-MM-dd"); word.Dnum = "ss-410922123"; ef.Entry(word).State = System.Data.Entity.EntityState.Added; flag = ef.SaveChanges(); } return flag; } //查询日志类别 [HttpGet] [Route("api/Search")] public List<DayType> Quert() { List<DayType> list = null; using (var ef = new MyContext()) { list = (from s in ef.DayType select s).ToList(); return list; } } [HttpGet] [Route("api/Page")] public PageModel PageData(int PageIndex = 1,int PageSize=2) { PageModel page = new PageModel(); List<ViewModel> models = null; using (var ef = new MyContext()) { //定义参数 SqlParameter[] para = new SqlParameter[] { new SqlParameter{ ParameterName="@PageIndex",SqlValue=PageIndex,SqlDbType = SqlDbType.Int}, new SqlParameter{ ParameterName="@PageSize",SqlValue=PageSize,SqlDbType = SqlDbType.Int}, new SqlParameter{ ParameterName="@PageCount",SqlDbType = SqlDbType.Int,Direction=ParameterDirection.Output}, }; models = ef.Database.SqlQuery<ViewModel>("exec Page_Week03 @PageIndex,@PageSize,@PageCount out", para).ToList(); page.Count = Convert.ToInt32(para[2].Value.ToString()); page.model = models; return page; } }