1、延迟加载
var query1 = _context.SysDataDict.Where(x => x.BaseVersion.Equals(0)).ToList(); var query2 = _context.SysDataDict.Where(x => x.BaseVersion.Equals(0)); Console.WriteLine("query1 count:"+ query1.Count); var a1 = new SysDataDict() { Id = 123, BaseIsDelete = 0, BaseCreatorId = 0, BaseModifierId = 0, BaseVersion = 0, DictType = "hah", DictSort = 1,BaseCreateTime=DateTime.Now,BaseModifyTime=DateTime.Now ,Remark=""}; _context.SysDataDict.Add(a1); _context.SaveChanges(); Console.WriteLine("query2 count:" + query2.ToList().Count);
2、默认筛选
modelBuilder.Entity<Table>.HasQueryFilter(x=>x.A2<5)
3、不映射字段
关键字[NotMapped]
4、级联删除
.OnDelete(DeleteBehavior.ClientCascade)
5、并发冲突
默认乐观并发,上下文获取数据后,跟数据库是没有关系了
IsRowVersion().IsConcurrencyToken();
6、批量删除
EF自带方法中,根据sqlserver跟踪可知,是一条条删除,在数据量比较大时,浪费数据库性能。
Nuget中获取包:EFCore.BulkExtensions
7、执行SQL
_context.Database.ExecuteSqlRaw()
注意防注入,几种方式都可以防止注入
A、占位符
B、@占位符
C、$@
参考视频链接:https://www.bilibili.com/video/BV1bV411x7Y1
标签:query2,实用技巧,SysDataDict,BaseVersion,EF,ORM,context,var From: https://www.cnblogs.com/sailing92/p/16801091.html