`
923723914
  • 浏览: 640035 次
文章分类
社区版块
存档分类
最新评论

EF的增删改查

 
阅读更多

EF的增删改查:

using System;

using System.Collections.Generic;

using System.Data.Entity.Infrastructure;

using System.Linq;

using System.Linq.Expressions;

using System.Text;

using System.Threading.Tasks;

namespace Dal

{

publicclassStudentsDal

{

zzEntities zzDB = new zzEntities();

publicintAdd(Student model)

{

zzDB.Students.Add(model);

returnzzDB.SaveChanges();

}

publicintDeleteStu(Student model) {

zzDB.Students.Attach(model);

zzDB.Students.Remove(model);

returnzzDB.SaveChanges();

}

publicintUpdateStu(Student model,string[]propertys) {

DbEntityEntry dbEE =zzDB.Entry(model);

dbEE.State = System.Data.EntityState.Unchanged;

foreach(var pro inpropertys)

{

dbEE.Property(pro).IsModified = true;

}

returnzzDB.SaveChanges();

}

publicintDeleteStuBy(Expression<Func<Student,bool>>delWhere) {

List<Student>stuList = zzDB.Students.Where(delWhere).ToList();

stuList.ForEach(stu =>zzDB.Students.Remove(stu));

returnzzDB.SaveChanges();

}

publicList<Student>GetListBy(Expression<Func<Student,bool>>stuWhere) {

return zzDB.Students.Where(stuWhere).ToList();

}

publicList<Student>GetPageList<Tkey>(intPageIndex,int PageSize,Expression<Func<Student,bool>>stuwhere,Expression<Func<Student,Tkey>> orderBy ) {

returnzzDB.Students.Where(stuwhere).OrderBy(orderBy).Skip((PageIndex-1)*PageSize).Take(PageSize).ToList();

}

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics