1:准备.tt模板
using BBFJ.OA.IBLL; using BBFJ.OA.IDAL; using BBFJ.OA.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BBFJ.OA.BLL { public partial class ActionInfoService :BaseService<ActionInfo>,IActionInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.ActionInfoDal; } } public partial class DepartmentService :BaseService<Department>,IDepartmentService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.DepartmentDal; } } public partial class R_UserInfo_ActionInfoService :BaseService<R_UserInfo_ActionInfo>,IR_UserInfo_ActionInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.R_UserInfo_ActionInfoDal; } } public partial class RoleInfoService :BaseService<RoleInfo>,IRoleInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.RoleInfoDal; } } public partial class UserInfoService :BaseService<UserInfo>,IUserInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.UserInfoDal; } } }
注意修改.tt模板中的
string inputFile = @"..\BBFJ.OA.Model\Model.edmx";
和
using BBFJ.OA.IBLL;
using BBFJ.OA.IDAL;
using BBFJ.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BBFJ.OA.BLL
3: 修改原类的属性为部分类 partial
public partial class UserInfoService : BaseService<UserInfo>,IUserInfoService
并注释掉重复内容
using BBFJ.OA.IBLL; using BBFJ.OA.IDAL; using BBFJ.OA.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BBFJ.OA.BLL { public partial class UserInfoService : BaseService<UserInfo>,IUserInfoService { //public override void SetCurrentDal() //{ // //子类拿到父类的CurrentDal属性 // //子类拿到父类的DBSession属性 // CurrentDal = this.CurrentDBSession.UserInfoDal; //} #region 02批量删除 public bool DeleteEntities(List<int> list) { //拿到父类的DBSession属性,拿到具体的数据操作类,调用查询方法 var userInfoList = this.CurrentDBSession.UserInfoDal.LoadEntities(u=>list.Contains(u.ID)); foreach (var userInfo in userInfoList) { this.CurrentDBSession.UserInfoDal.DeleteEntity(userInfo); } return this.CurrentDBSession.SaveChanges(); } #endregion #region 03完成用户信息的搜索 public IQueryable<UserInfo> LoadSearchEntities(Model.Search.UserInfoSearch userInfoSearch,short delFlag) { var temp = this.CurrentDBSession.UserInfoDal.LoadEntities(c=>c.DelFlag == delFlag ); //根据用户名搜索 if (!String.IsNullOrEmpty(userInfoSearch.UserName)) { temp = temp.Where<UserInfo>(u=>u.UName.Contains(userInfoSearch.UserName)); } if (!String.IsNullOrEmpty(userInfoSearch.UserRemark)) { temp = temp.Where<UserInfo>(u => u.Remark.Contains(userInfoSearch.UserRemark)); } userInfoSearch.TotalCount = temp.Count(); return temp.OrderBy<UserInfo, int>(u => u.ID).Skip<UserInfo>((userInfoSearch.PageIndex - 1) * userInfoSearch.PageSize).Take<UserInfo>(userInfoSearch.PageSize); } #endregion } }
4: 常见的错误:
修改方法 public partial class UserInfoService : BaseService<UserInfo>,IUserInfoService 类前加"partial" 关键字
注释到重复方法
最终生成代码
using BBFJ.OA.IBLL; using BBFJ.OA.IDAL; using BBFJ.OA.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BBFJ.OA.BLL { public partial class ActionInfoService :BaseService<ActionInfo>,IActionInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.ActionInfoDal; } } public partial class DepartmentService :BaseService<Department>,IDepartmentService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.DepartmentDal; } } public partial class R_UserInfo_ActionInfoService :BaseService<R_UserInfo_ActionInfo>,IR_UserInfo_ActionInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.R_UserInfo_ActionInfoDal; } } public partial class RoleInfoService :BaseService<RoleInfo>,IRoleInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.RoleInfoDal; } } public partial class UserInfoService :BaseService<UserInfo>,IUserInfoService { public override void SetCurrentDal() { CurrentDal = this.CurrentDBSession.UserInfoDal; } } }