书接上文,继续搭建我们基于.netCore 的开发框架。首先是我们的项目分层结构。
这个分层结构,是参考张老师的分层结构,但是实际项目中,我没有去实现仓储模型。因为我使用的是EFCore ,最近也一直在想,EFCore 在我们的架构体系中到底扮演着什么样的角色??
当然,实现仓储层,也有他的好处,如果真的以后要更换ORM框架的时候,不用去更改服务层逻辑,不用直接在仓储里面做更改就可以了。但是对于小项目,可能运行个十年都不会换数据库,不会换ORM的项目,仓储层的意义在哪?
希望对此有自己想法的朋友一起讨论。在本系列里,我将保留仓储层。
上面的分层结构,很容易就搭建好了,但是基于.NetCore 的项目,可能最主要的一点就是如何利用好DI,怎么来实现依赖注入。
依照上图中的依赖关系,我们以此构建自己相应层级中的内容。
分别在IRepository和Repository项目下,新建Base文件夹,并分别建立IBaseRepository和BaseRepository
具体代码如下:
IBaseRepository.cs:
1 using Sincere.Core.Model; 2 using System; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Linq.Expressions; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace Sincere.Core.IRepository.Base 11 { 12 public interface IBaseRepository<TEntity> where TEntity : class 13 { 14 Task<int> Execute(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text); 15 Task<List<TEntity>> Query(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text); 16 Task<bool> Insert(TEntity model); 17 Task<bool> InsertRange(List<TEntity> datas); 18 19 Task<int> Del(TEntity model); 20 21 Task<int> DelBy(Expression<Func<TEntity, bool>> delWhere); 22 23 Task<int> Modify(TEntity model); 24 25 Task<int> Modify(TEntity model, params string[] propertyNames); 26 27 Task<int> ModifyBy(TEntity model, Expression<Func<TEntity, bool>> whereLambda, params string[] modifiedPropertyNames); 28 29 Task<List<TEntity>> GetList(); 30 31 Task<List<TEntity>> GetListBy(Expression<Func<TEntity, bool>> whereLambda); 32 33 Task<TEntity> GetModelById(Expression<Func<TEntity, bool>> whereLambda); 34 35 Task<List<TEntity>> GetListBy<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true); 36 37 Task<List<TEntity>> GetListBy<TKey>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true); 38 39 Task<List<TEntity>> GetListBy<TKey1, TKey2>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true); 40 41 Task<List<TEntity>> GetListBy<TKey1, TKey2>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true); 42 43 Task<List<TEntity>> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true); 44 45 Task<PageModel<TEntity>> GetPagedList<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true, int pageIndex = 1, int pageSize = 20); 46 47 void RollBackChanges(); 48 49 } 50 }
BaseRepository.cs:
1 using Microsoft.EntityFrameworkCore; 2 using Sincere.Core.IRepository.Base; 3 using Sincere.Core.Model; 4 using Sincere.Core.Model.Models; 5 using System; 6 using System.Collections.Generic; 7 using System.Data; 8 using System.Data.SqlClient; 9 using System.Linq; 10 using System.Linq.Expressions; 11 using System.Reflection; 12 using System.Text; 13 using System.Threading.Tasks; 14 15 namespace Sincere.Core.Repository.Base 16 { 17 public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new() 18 { 19 private BaseCoreContext _db; 20 private readonly DbSet<TEntity> _dbSet; 21 22 internal BaseCoreContext Db 23 { 24 get { return _db; } 25 private set { _db = value; } 26 } 27 public BaseRepository(IBaseContext mydbcontext) 28 { 29 this._db = mydbcontext as BaseCoreContext; 30 this._dbSet = _db.Set<TEntity>(); 31 } 32 33 #region INSERT 34 35 /// <summary> 36 /// 新增 实体 37 /// </summary> 38 /// <param name="model"></param> 39 /// <returns></returns> 40 //public async Task<bool> Insert(TEntity model, bool isSaveChanges = false) 41 public async Task<bool> Insert(TEntity model) 42 { 43 _db.Set<TEntity>().Add(model); 44 //if (isSaveChanges) 45 //{ 46 return await _db.SaveChangesAsync() > 0; 47 //} 48 //else 49 //{ 50 // return false; 51 //} 52 53 } 54 55 /// <summary> 56 /// 普通批量插入 57 /// </summary> 58 /// <param name="datas"></param> 59 public async Task<bool> InsertRange(List<TEntity> datas) 60 { 61 await _db.Set<TEntity>().AddRangeAsync(datas); 62 return await _db.SaveChangesAsync() == datas.Count; 63 } 64 65 #endregion INSERT 66 67 #region Delete 68 69 #region 2.0 根据id删除 + int Del(T model) 70 /// <summary> 71 /// 2.0 根据id删除 72 /// </summary> 73 /// <param name="model">必须包含要删除id的对象</param> 74 /// <returns></returns> 75 public async Task<int> Del(TEntity model) 76 { 77 _db.Set<TEntity>().Attach(model); 78 _db.Set<TEntity>().Remove(model); 79 return await _db.SaveChangesAsync(); 80 } 81 #endregion 82 83 #region 2.1 根据条件删除 + int DelBy(Expression<Func<T, bool>> delWhere) 84 /// <summary> 85 /// 2.1 根据条件删除 86 /// </summary> 87 /// <param name="delWhere"></param> 88 /// <returns>返回受影响的行数</returns> 89 public async Task<int> DelBy(Expression<Func<TEntity, bool>> delWhere) 90 { 91 //2.1.1 查询要删除的数据 92 List<TEntity> listDeleting = _db.Set<TEntity>().Where(delWhere).ToList(); 93 //2.1.2 将要删除的数据 用删除方法添加到 EF 容器中 94 listDeleting.ForEach(u => 95 { 96 _db.Set<TEntity>().Attach(u); //先附加到EF 容器 97 _db.Set<TEntity>().Remove(u); //标识为删除状态 98 }); 99 //2.1.3 一次性生成sql语句 到数据库执行删除 100 return await _db.SaveChangesAsync(); 101 } 102 #endregion 103 104 105 #endregion 106 107 #region UPDATE 108 109 #region 3.0 修改实体 + int Modify(T model) 110 /// <summary> 111 /// 修改实体 112 /// </summary> 113 /// <param name="model"></param> 114 /// <returns></returns> 115 public async Task<int> Modify(TEntity model) 116 { 117 //EntityEntry entry = _db.Entry<TEntity>(model); 118 _db.Set<TEntity>().Update(model); 119 return await _db.SaveChangesAsync(); 120 } 121 #endregion 122 123 #region 3.1 修改实体,可修改指定属性 + int Modify(T model, params string[] propertyNames) 124 /// <summary> 125 /// 3.1 修改实体,可修改指定属性 126 /// </summary> 127 /// <param name="model"></param> 128 /// <param name="propertyName"></param> 129 /// <returns></returns> 130 public async Task<int> Modify(TEntity model, params string[] propertyNames) 131 { 132 //3.1.1 将对象添加到EF中 133 EntityEntry entry = _db.Entry<TEntity>(model); 134 //3.1.2 先设置对象的包装状态为 Unchanged 135 entry.State = EntityState.Unchanged; 136 //3.1.3 循环被修改的属性名数组 137 foreach (string propertyName in propertyNames) 138 { 139 //将每个被修改的属性的状态设置为已修改状态;这样在后面生成的修改语句时,就只为标识为已修改的属性更新 140 entry.Property(propertyName).IsModified = true; 141 } 142 return await _db.SaveChangesAsync(); 143 } 144 #endregion 145 146 #region 3.2 批量修改 + int ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedPropertyNames) 147 /// <summary> 148 /// 3.2 批量修改 149 /// </summary> 150 /// <param name="model"></param> 151 /// <param name="whereLambda"></param> 152 /// <param name="modifiedPropertyNames"></param> 153 /// <returns></returns> 154 public async Task<int> ModifyBy(TEntity model, Expression<Func<TEntity, bool>> whereLambda, params string[] modifiedPropertyNames) 155 { 156 //3.2.1 查询要修改的数据 157 List<TEntity> listModifing = _db.Set<TEntity>().Where(whereLambda).ToList(); 158 //3.2.2 获取实体类类型对象 159 Type t = typeof(TEntity); 160 //3.2.3 获取实体类所有的公共属性 161 List<PropertyInfo> propertyInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); 162 //3.2.4 创建实体属性字典集合 163 Dictionary<string, PropertyInfo> dicPropertys = new Dictionary<string, PropertyInfo>(); 164 //3.2.5 将实体属性中要修改的属性名 添加到字典集合中 键:属性名 值:属性对象 165 propertyInfos.ForEach(p => 166 { 167 if (modifiedPropertyNames.Contains(p.Name)) 168 { 169 dicPropertys.Add(p.Name, p); 170 } 171 }); 172 //3.2.6 循环要修改的属性名 173 foreach (string propertyName in modifiedPropertyNames) 174 { 175 //判断要修改的属性名是否在实体类的属性集合中存在 176 if (dicPropertys.ContainsKey(propertyName)) 177 { 178 //如果存在,则取出要修改的属性对象 179 PropertyInfo proInfo = dicPropertys[propertyName]; 180 //取出要修改的值 181 object newValue = proInfo.GetValue(model, null); 182 //批量设置要修改对象的属性 183 foreach (TEntity item in listModifing) 184 { 185 //为要修改的对象的要修改的属性设置新的值 186 proInfo.SetValue(item, newValue, null); 187 } 188 } 189 } 190 //一次性生成sql语句 到数据库执行 191 return await _db.SaveChangesAsync(); 192 } 193 #endregion 194 195 196 #endregion UPDATE 197 198 #region SELECT 199 200 #region 5.0 根据条件查询 + List<TEntity> GetListBy(Expression<Func<T, bool>> whereLambda) 201 /// <summary> 202 /// 5.0 根据条件查询 203 /// </summary> 204 /// <param name="whereLambda"></param> 205 /// <returns></returns> 206 public async Task<List<TEntity>> GetListBy(Expression<Func<TEntity, bool>> whereLambda) 207 { 208 return await _db.Set<TEntity>().Where(whereLambda).AsNoTracking().ToListAsync(); 209 } 210 211 public async Task<TEntity> GetModelById(Expression<Func<TEntity, bool>> whereLambda) 212 { 213 return await _db.Set<TEntity>().Where(whereLambda).AsNoTracking().FirstOrDefaultAsync(); 214 } 215 public async Task<List<TEntity>> GetList() 216 { 217 return await _db.Set<TEntity>().AsNoTracking().ToListAsync(); 218 } 219 #endregion 220 221 #region 5.1 根据条件查询,并排序 + List<TEntity> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda, bool isAsc = true) 222 /// <summary> 223 /// 5.1 根据条件查询,并排序 224 /// </summary> 225 /// <typeparam name="TKey"></typeparam> 226 /// <param name="whereLambda"></param> 227 /// <param name="orderLambda"></param> 228 /// <param name="isAsc"></param> 229 /// <returns></returns> 230 public async Task<List<TEntity>> GetListBy<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true) 231 { 232 if (isAsc) 233 { 234 return await _db.Set<TEntity>().Where(whereLambda).OrderBy(orderLambda).AsNoTracking().ToListAsync(); 235 } 236 else 237 { 238 return await _db.Set<TEntity>().Where(whereLambda).OrderByDescending(orderLambda).AsNoTracking().ToListAsync(); 239 } 240 } 241 #endregion 242 243 #region 5.2 根据条件查询Top多少个,并排序 + List<TEntity> GetListBy<TKey>(int top, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda, bool isAsc = true) 244 /// <summary> 245 /// 5.2 根据条件查询Top多少个,并排序 246 /// </summary> 247 /// <typeparam name="TKey"></typeparam> 248 /// <param name="top"></param> 249 /// <param name="whereLambda"></param> 250 /// <param name="orderLambda"></param> 251 /// <param name="isAsc"></param> 252 /// <returns></returns> 253 public async Task<List<TEntity>> GetListBy<TKey>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderLambda, bool isAsc = true) 254 { 255 if (isAsc) 256 { 257 return await _db.Set<TEntity>().Where(whereLambda).OrderBy(orderLambda).Take(top).AsNoTracking().ToListAsync(); 258 } 259 else 260 { 261 return await _db.Set<TEntity>().Where(whereLambda).OrderByDescending(orderLambda).Take(top).AsNoTracking().ToListAsync(); 262 } 263 } 264 #endregion 265 266 #region 5.3 根据条件排序查询 双排序 + List<TEntity> GetListBy<TKey1, TKey2>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey1>> orderLambda1, Expression<Func<T, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true) 267 /// <summary> 268 /// 5.3 根据条件排序查询 双排序 269 /// </summary> 270 /// <typeparam name="TKey1"></typeparam> 271 /// <typeparam name="TKey2"></typeparam> 272 /// <param name="whereLambda"></param> 273 /// <param name="orderLambda1"></param> 274 /// <param name="orderLambda2"></param> 275 /// <param name="isAsc1"></param> 276 /// <param name="isAsc2"></param> 277 /// <returns></returns> 278 public async Task<List<TEntity>> GetListBy<TKey1, TKey2>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true) 279 { 280 if (isAsc1) 281 { 282 if (isAsc2) 283 { 284 return await _db.Set<TEntity>().Where(whereLambda).OrderBy(orderLambda1).ThenBy(orderLambda2).AsNoTracking().ToListAsync(); 285 } 286 else 287 { 288 return await _db.Set<TEntity>().Where(whereLambda).OrderBy(orderLambda1).ThenByDescending(orderLambda2).AsNoTracking().ToListAsync(); 289 } 290 } 291 else 292 { 293 if (isAsc2) 294 { 295 return await _db.Set<TEntity>().Where(whereLambda).OrderByDescending(orderLambda1).ThenBy(orderLambda2).AsNoTracking().ToListAsync(); 296 } 297 else 298 { 299 return await _db.Set<TEntity>().Where(whereLambda).OrderByDescending(orderLambda1).ThenByDescending(orderLambda2).AsNoTracking().ToListAsync(); 300 } 301 } 302 } 303 #endregion 304 305 #region 5.3 根据条件排序查询Top个数 双排序 + List<TEntity> GetListBy<TKey1, TKey2>(int top, Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TKey1>> orderLambda1, Expression<Func<T, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true) 306 /// <summary> 307 /// 5.3 根据条件排序查询Top个数 双排序 308 /// </summary> 309 /// <typeparam name="TKey1"></typeparam> 310 /// <typeparam name="TKey2"></typeparam> 311 /// <param name="top"></param> 312 /// <param name="whereLambda"></param> 313 /// <param name="orderLambda1"></param> 314 /// <param name="orderLambda2"></param> 315 /// <param name="isAsc1"></param> 316 /// <param name="isAsc2"></param> 317 /// <returns></returns> 318 public async Task<List<TEntity>> GetListBy<TKey1, TKey2>(int top, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey1>> orderLambda1, Expression<Func<TEntity, TKey2>> orderLambda2, bool isAsc1 = true, bool isAsc2 = true) 319 { 320 if (isAsc1) 321 { 322 if (isAsc2) 323 { 324 return await _db.Set<TEntity>().Where(whereLambda).OrderBy(orderLambda1).ThenBy(orderLambda2).Take(top).AsNoTracking().ToListAsync(); 325 } 326 else 327 { 328 return await _db.Set<TEntity>().Where(whereLambda).OrderBy(orderLambda1).ThenByDescending(orderLambda2).Take(top).AsNoTracking().ToListAsync(); 329 } 330 } 331 else 332 { 333 if (isAsc2) 334 { 335 return await _db.Set<TEntity>().Where(whereLambda).OrderByDescending(orderLambda1).ThenBy(orderLambda2).Take(top).AsNoTracking().ToListAsync(); 336 } 337 else 338 { 339 return await _db.Set<TEntity>().Where(whereLambda).OrderByDescending(orderLambda1).ThenByDescending(orderLambda2).Take(top).AsNoTracking().ToListAsync(); 340 } 341 } 342 } 343 #endregion 344 345 #endregion SELECT 346 347 #region 分页 348 349 #region 6.0 分页查询 + List<T> GetPagedList<TKey> 350 /// <summary> 351 /// 分页查询 + List<TEntity> GetPagedList 352 /// </summary> 353 /// <typeparam name="TKey"></typeparam> 354 /// <param name="pageIndex">页码</param> 355 /// <param name="pageSize">页容量</param> 356 /// <param name="whereLambda">条件 lambda表达式</param> 357 /// <param name="orderBy">排序 lambda表达式</param> 358 /// <returns></returns> 359 public async Task<List<TEntity>> GetPagedList<TKey>(int pageIndex, int pageSize, Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true) 360 { 361 // 分页 一定注意: Skip 之前一定要 OrderBy 362 if (isAsc) 363 { 364 return await _db.Set<TEntity>().Where(whereLambda).OrderBy(orderByLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking().ToListAsync(); 365 } 366 else 367 { 368 return await _db.Set<TEntity>().Where(whereLambda).OrderByDescending(orderByLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking().ToListAsync(); 369 } 370 } 371 #endregion 372 373 #region 6.1分页查询 带输出 +List<TEntity> GetPagedList<TKey> 374 /// <summary> 375 /// 分页查询 带输出 376 /// </summary> 377 /// <typeparam name="TKey"></typeparam> 378 /// <param name="pageIndex"></param> 379 /// <param name="pageSize"></param> 380 /// <param name="rowCount"></param> 381 /// <param name="whereLambda"></param> 382 /// <param name="orderBy"></param> 383 /// <param name="isAsc"></param> 384 /// <returns></returns> 385 public async Task<PageModel<TEntity>> GetPagedList<TKey>(Expression<Func<TEntity, bool>> whereLambda, Expression<Func<TEntity, TKey>> orderByLambda, bool isAsc = true, int pageIndex = 1, int pageSize = 20) 386 { 387 var rowCount = await _db.Set<TEntity>().Where(whereLambda).CountAsync(); 388 389 int pageCount = (Math.Ceiling(rowCount.ObjToDecimal() / pageSize.ObjToDecimal())).ObjToInt(); 390 391 List<TEntity> list = null; 392 393 if (isAsc) 394 { 395 list = await _db.Set<TEntity>().OrderBy(orderByLambda).Where(whereLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking().ToListAsync(); 396 } 397 else 398 { 399 list = await _db.Set<TEntity>().OrderByDescending(orderByLambda).Where(whereLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsNoTracking().ToListAsync(); 400 } 401 402 return new PageModel<TEntity>() { dataCount = rowCount, pageCount = pageCount, page = pageIndex, PageSize = pageSize, data = list }; 403 } 404 #endregion 405 406 #endregion 407 408 #region ORTHER 409 410 /// <summary> 411 /// 执行存储过程或自定义sql语句--返回集合 412 /// </summary> 413 /// <param name="sql"></param> 414 /// <param name="parms"></param> 415 /// <param name="cmdType"></param> 416 /// <returns></returns> 417 public async Task<List<TEntity>> Query(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text) 418 { 419 //存储过程(exec getActionUrlId @name,@ID) 420 if (cmdType == CommandType.StoredProcedure) 421 { 422 StringBuilder paraNames = new StringBuilder(); 423 foreach (var sqlPara in parms) 424 { 425 paraNames.Append($" @{sqlPara},"); 426 } 427 sql = paraNames.Length > 0 ? $"exec {sql} {paraNames.ToString().Trim(',')}" : $"exec {sql} "; 428 } 429 430 return await _db.Set<TEntity>().FromSql(sql).ToListAsync(); 431 432 } 433 434 435 /// <summary> 436 /// 回滚 437 /// </summary> 438 public void RollBackChanges() 439 { 440 var items = _db.ChangeTracker.Entries().ToList(); 441 items.ForEach(o => o.State = EntityState.Unchanged); 442 } 443 444 /// <summary> 445 /// 自定义语句和存储过程的增删改--返回影响的行数 446 /// </summary> 447 /// <param name="sql"></param> 448 /// <param name="parms"></param> 449 /// <param name="cmdType"></param> 450 /// <returns></returns> 451 public async Task<int> Execute(string sql, List<SqlParameter> parms, CommandType cmdType = CommandType.Text) 452 { 453 //存储过程(exec getActionUrlId @name,@ID) 454 if (cmdType == CommandType.StoredProcedure) 455 { 456 StringBuilder paraNames = new StringBuilder(); 457 foreach (var sqlPara in parms) 458 { 459 paraNames.Append($" @{sqlPara},"); 460 } 461 sql = paraNames.Length > 0 ? 462 $"exec {sql} {paraNames.ToString().Trim(',')}" : 463 $"exec {sql} "; 464 } 465 466 int ret = await _db.Database.ExecuteSqlCommandAsync(sql, parms.ToArray()); 467 return 0; 468 } 469 470 471 #endregion ORTHER 472 } 473 }
BaseRepository中主要是实现了一些基础的CURD操作,并且封装了一些常用的数据库操作。比如分页、执行sql语句等等
如果直接复制以上代码,会报错,因为你的项目中还没有数据上下文:
下面就引入我们将要使用的ORM框架--EFCore
首先,在model层总引入:
Install-Package Microsoft.EntityFrameworkCore -version 2.2.4
Install-Package Microsoft.EntityFrameworkCore.SqlServer -version 2.2.4
Install-Package Microsoft.EntityFrameworkCore.Tools -version 2.2.4
此系类中我们采用databaseFrist的方式来生成Model文件和上下文。在数据库中先建用于测试的表--Advertisement
语句如下:
1 USE [BaseCore] 2 GO 3 4 /****** Object: Table [dbo].[Advertisement] Script Date: 08/28/2019 10:43:34 ******/ 5 SET ANSI_NULLS ON 6 GO 7 8 SET QUOTED_IDENTIFIER ON 9 GO 10 11 CREATE TABLE [dbo].[Advertisement]( 12 [Id] [int] IDENTITY(1,1) NOT NULL, 13 [Createdate] [datetime] NOT NULL, 14 [ImgUrl] [nvarchar](512) NULL, 15 [Title] [nvarchar](64) NULL, 16 [Url] [nvarchar](256) NULL, 17 [Remark] [nvarchar](max) NULL, 18 CONSTRAINT [PK_dbo.Advertisement] PRIMARY KEY CLUSTERED 19 ( 20 [Id] ASC 21 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 22 ) ON [PRIMARY] 23 24 GO
然后使用工具Scaffold-DbContext(数据库上下文脚手架)来生成model类文件和DbContext。
命令如下:
Scaffold-DbContext "server=.;database=XXX;uid=sa;pwd=密码;" Microsoft.EntityFrameworkCore.SqlServer -O Models -F -Context BaseCoreContext
在执行这个命令之前,需要将Model层设置为启动项目。否则会报错。
另外当你的项目Buid没有通过的话,会出现如下错误。
我一般的解决方案就是将其他项目全部先卸载,只留下一个Model,然后再执行以上语句。
这时,在你的Model项目下的Models文件夹下,就会出现如下文件:
打开BaseCoreContext.cs 文件,删除OnConfiguring 方法,至于为什么,我会在后面说明。
为了让数据库上下文也能用DI的方式进行使用,我新建了一个, IBaseContext接口,并且让BaseCoreContext 实现这个接口。
这个时候,仓储的基础父类应该是不会报错。但是我在BaseCoreContext里实现了一个分页操作,用到了一个PageModel 。需要添加到你的Model层里面
代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace Sincere.Core.Model 6 { 7 /// <summary> 8 /// 通用分页信息类 9 /// </summary> 10 public class PageModel<T> 11 { 12 /// <summary> 13 /// 当前页标 14 /// </summary> 15 public int page { get; set; } = 1; 16 /// <summary> 17 /// 总页数 18 /// </summary> 19 public int pageCount { get; set; } = 6; 20 /// <summary> 21 /// 数据总数 22 /// </summary> 23 public int dataCount { get; set; } = 0; 24 /// <summary> 25 /// 每页大小 26 /// </summary> 27 public int PageSize { set; get; } 28 /// <summary> 29 /// 返回数据 30 /// </summary> 31 public List<T> data { get; set; } 32 33 } 34 }
另外里面还有一些 转换函数,请参考源码:https://github.com/xzhencheng/Sincere.Core.git
至此,项目的仓储层的基础代码就构建完了,今天的作业就先写到这!