1.1.0 create data layer
1.1.1 CZBK.ItcastOA.IDAL refers to CZBK.ItcastOA.Model
1.2.1 add an interface IUserInfoDal to IDAL
Interface method for adding, deleting, modifying and querying pages in
public interface IUserInfoDal { //check IQueryable<UserInfo> LoadEntities(System.Linq.Expressions.Expression<Func<UserInfo,bool>> whereLambda); //paging //<s>The generic type of the method, which is passed when the method is called IQueryable<UserInfo> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<UserInfo,bool>>whereLambda, System.Linq.Expressions.Expression<Func<UserInfo, s>>orderbyLambda, bool isAsc); //increase UserInfo AddEntity(UserInfo entity); //Delete bool EditEntity(UserInfo entity); //change bool DeleteEntity(UserInfo entity); }
1.3.1 add an interface IBaseDal to IDAL, cut the code of IUserInfoDal, and replace UserInfo with T
public interface IBaseDal<T>where T:class,new() { //check IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda); //paging //<s>The generic type of the method, which is passed when the method is called IQueryable<T> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderbyLambda, bool isAsc); //increase T AddEntity(T entity); //Delete bool EditEntity(T entity); //change bool DeleteEntity(T entity); }
1.4.1 let IUserInfoDal interface inherit IBaseDal, and only its unique methods are defined in IUserInfoDal interface
public interface IUserInfoDal:IBaseDal<UserInfo> { //Define your own approach }
2.1.1 CZBK.ItcastOA.DAL refers to IDAL and Model, and adds a class UserInfoDal
2.2.1 UserInfoDal inherits and implements IUserInfoDal
2.3.1 get OAEntities of operation data first
2.3.2 create a data operation object Db, but Db.UserInfo. Cannot be clicked
Because DAL did not introduce MVC
2.4.1 introduce EF by creating a new empty entity model
In 2.4.2, the following files are introduced to delete the generated Model1.cs. At this time, Db can be used
2.5.1 UserInfoDal's method of adding, deleting, modifying and querying pages
public class UserInfoDal : IUserInfoDal { OAEntities Db = new OAEntities(); /// <summary> /// newly added /// </summary> /// <param name="entity"></param> /// <returns></returns> public UserInfo AddEntity(UserInfo entity) { Db.UserInfo.Add(entity); Db.SaveChanges(); return entity; } /// <summary> /// delete /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(UserInfo entity) { //Append to ef On, mark the deletion mark, and then save Db.Entry<UserInfo>(entity).State = System.Data.Entity.EntityState.Deleted; return Db.SaveChanges() > 0; } /// <summary> /// to update /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool EditEntity(UserInfo entity) { //Append to ef And then save it Db.Entry<UserInfo>(entity).State = System.Data.Entity.EntityState.Modified; return Db.SaveChanges() > 0; } /// <summary> /// Query filtering /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public IQueryable<UserInfo> LoadEntities(Expression<Func<UserInfo, bool>> whereLambda) { return Db.UserInfo.Where(whereLambda); } /// <summary> /// paging /// </summary> /// <typeparam name="s"></typeparam> /// <param name="pageIndex">Page number</param> /// <param name="pageSize">Quantity per page</param> /// <param name="totalCount">Total</param> /// <param name="whereLambda">Filter condition</param> /// <param name="orderbyLambda">sort criteria</param> /// <param name="isAsc"></param> /// <returns></returns> public IQueryable<UserInfo> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<UserInfo, bool>> whereLambda, Expression<Func<UserInfo, s>> orderbyLambda, bool isAsc) { var temp = Db.UserInfo.Where(whereLambda); totalCount = temp.Count(); if(isAsc)//positive sequence { //example: pageIndex=3,pageSize=15 //Skip data before page 3 after positive sequence(First 2 pages*15),All that's left is 15 temp = temp.OrderBy<UserInfo, s>(orderbyLambda).Skip<UserInfo>((pageIndex - 1) * pageSize).Take<UserInfo>(pageSize); } else//Reverse order { temp = temp.OrderByDescending<UserInfo, s>(orderbyLambda).Skip<UserInfo>((pageIndex - 1) * pageSize).Take<UserInfo>(pageSize); } return temp; } }
2.6.1 each page needs to implement the method of adding, deleting, modifying and querying pages, so you can create a new BaseDal, cut them here, and encapsulate the specific implementation of public methods
Replace UserInfo with T
There is no such writing method for Db.T.Add(). Instead, DB. Set < T > (). Add() returns dbset < T >
EF can operate data because this dbset < T >
2.6.2 BaseDal does not need to inherit IBaseDal, because it has implemented all the public methods in IBaseDal
public class BaseDal<T> where T : class, new() { OAEntities Db = new OAEntities(); /// <summary> /// newly added /// </summary> /// <param name="entity"></param> /// <returns></returns> public T AddEntity(T entity) { Db.Set<T>().Add(entity);//DbSet<T> Db.SaveChanges(); return entity; } /// <summary> /// delete /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(T entity) { //Append to ef On, mark the deletion mark, and then save Db.Entry<T>(entity).State = System.Data.Entity.EntityState.Deleted; return Db.SaveChanges() > 0; } /// <summary> /// to update /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool EditEntity(T entity) { //Append to ef And then save it Db.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified; return Db.SaveChanges() > 0; } /// <summary> /// Query filtering /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda) { return Db.Set<T>().Where(whereLambda); } /// <summary> /// paging /// </summary> /// <typeparam name="s"></typeparam> /// <param name="pageIndex">Page number</param> /// <param name="pageSize">Quantity per page</param> /// <param name="totalCount">Total</param> /// <param name="whereLambda">Filter condition</param> /// <param name="orderbyLambda">sort criteria</param> /// <param name="isAsc"></param> /// <returns></returns> public IQueryable<T> PageLoadEntities<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderbyLambda, bool isAsc) { var temp = Db.Set<T>().Where(whereLambda); totalCount = temp.Count(); if (isAsc)//positive sequence { //Column: pageIndex=3,pageSize=15 //Skip data before page 3 after positive sequence(First 2 pages*15),All that's left is 15 temp = temp.OrderBy<T, s>(orderbyLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize); } else//Reverse order { temp = temp.OrderByDescending<T, s>(orderbyLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize); } return temp; } }
2.7.1 UserInfoDal inherits the public method of BaseDal and the user-defined method of IUserInfoDal
public class UserInfoDal : BaseDal<UserInfo>,IUserInfoDal { //Inherited common methods, and also inherited custom methods //because BaseDal It's done IBaseDal No need to implement IUserInfo Inherited IBaseDal Public method of }