为了加深对EF特性的了解so写了一些测试代码测试结果也许对实际项目没什么用处但是对理解EF的相关机制还是有一定帮助的本文可能会不定期更新(加入新的测试用例==) 一事务 直接看代码 所有SaveChange包裹在一个TransactionScope里面 [TestMethod] public void TestMethod() { using (var entities = new SysProcessEntities()) { using (TransactionScope scope = new TransactionScope()) { try { var test = entitiesSysUserWhere(o => oOrganizationID == )ToList(); var test = entitiesSysRoleToList();
test[]Code = admin; test[]Name = dddd; entitiesSaveChanges();
test[]Code = admin; test[]Name = 全功能; entitiesSaveChanges();
scopeComplete(); } catch (Exception e) {
} } } } 结果 在的基础上去掉TransactionScope [TestMethod] public void TestMethod() { using (var entities = new SysProcessEntities()) { var test = entitiesSysUserWhere(o => oOrganizationID == )ToList(); var test = entitiesSysRoleToList();
test[]Code = admin; test[]Name = dddd; entitiesSaveChanges();
test[]Code = admin; test[]Name = 全功能; entitiesSaveChanges(); } } 结果 注释掉的第行和第行检查单独的一条语句是否自带事务 [TestMethod] public void TestMethod() { using (var entities = new SysProcessEntities()) { var test = entitiesSysUserWhere(o => oOrganizationID == )ToList(); var test = entitiesSysRoleToList();
//test[]Code = admin; test[]Name = dddd; entitiesSaveChanges();
//test[]Code = admin; test[]Name = 全功能; entitiesSaveChanges(); } } 结果 直接写Sqlupdate [SysProcess][dbo][SysRole] set [name]=全功能 where ID=在查询分析器中执行结果 将改为跨数据库 [TestMethod] public void TestMethod() { using (TransactionScope scope = new TransactionScope()) { try { using (var entities = new SysProcessEntities()) { var test = entitiesSysUserWhere(o => oOrganizationID == )ToList(); var test = entitiesSysRoleToList();
test[]Code = admin; test[]Name = dddd; entitiesSaveChanges();
test[]Code = admin; test[]Name = 全功能; entitiesSaveChanges(); } using (var entities = new DistributionEntities()) { var test = entitiesVIPCardToList(); test[]Sex = true; entitiesSaveChanges(); }
scopeComplete(); } catch (Exception e) { string msg = eMessage; } } } 结果 于是整个世界美好了…… 二AsNoTracking 注意AsNoTracking要写在最终返回数据的那行代码中才有用看代码 [TestMethod] public void TestMethod() { using (var entities = new SysProcessEntities()) { var ubs = entitiesUserBrandAsNoTracking(); var obs = entitiesOrganizationBrandWhere(ob => obOrganizationID == )AsNoTracking(); var brands = from ub in ubs from ob in obs where ubBrandID == obBrandID select ubBrandID; var test = entitiesProBrandWhere(b => brandsContains(bID))ToList(); AssertAreEqual(EntityStateDetached entitiesEntry(test[])State); } } 结果 sousing之中应该这么写 var ubs = entitiesUserBrand; var obs = entitiesOrganizationBrandWhere(ob => obOrganizationID == ); var brands = from ub in ubs from ob in obs where ubBrandID == obBrandID select ubBrandID; var test = entitiesProBrandWhere(b => brandsContains(bID))AsNoTracking()ToList(); AssertAreEqual(EntityStateDetached entitiesEntry(test[])State); 三DbSetLocal 继续看代码
public void TestMethod() { var entities = new SysProcessEntities(); var t = entitiesProBoduanToList(); var t = entitiesProBoduanToList(); AssertAreEqual(t[]t[]); } 大家以为t[]是否等于t[]?答案是true可是作为引用类型我并没有重载它的Equals方法照理应该为false才对呀修改下测试代码 public void TestMethod() { var entities = new SysProcessEntities(); var t = entitiesProBoduanToList(); var t = entitiesProBoduanToList(); string name = t[]Name; t[]Name = 随便取个名用来测试; AssertAreEqual(name t[]Name); } 结果 可知t[]和t[]指向的是同一个对象即t和t指向同一个数组地址也就是entitiesProBoduanLocal指向的地址不过数据库中仍执行了两次取数操作 then加入AsNoTracking试试看 public void TestMethod(){ var entities = new SysProcessEntities(); var t = entitiesProBoduanAsNoTracking()ToList(); var t = entitiesProBoduanAsNoTracking()ToList(); string name = t[]Name; t[]Name = 随便取个名用来测试; AssertAreEqual(name t[]Name); //truet的更改不影响t表明t和t指向不同地址} 此时entitiesProBoduanLocalCount为
public void TestMethod() { var entities = new SysProcessEntities(); var t = entitiesProBoduanFirstOrDefault(); var count = entitiesProBoduanLocalCount;// var t = entitiesProBoduanToList(); var count = entitiesProBoduanLocalCount;// } 将第行和第行换下位置 public void TestMethod() { var entities = new SysProcessEntities(); var t = entitiesProBoduanToList(); var count = entitiesProBoduanLocalCount;// var t = entitiesProBoduanFirstOrDefault(); var count = entitiesProBoduanLocalCount;// }
public void TestMethod() {var entities = new SysProcessEntities(); var t = entitiesProBoduanFirstOrDefault();var count = entitiesProBoduanLocalCount;//var t = entitiesProBoduanToList();var count = entitiesProBoduanLocalCount;// } 将第行和第行换下位置 public void TestMethod() {var entities = new SysProcessEntities();var t = entitiesProBoduanToList();var count = entitiesProBoduanLocalCount;// var t = entitiesProBoduanFirstOrDefault();var count = entitiesProBoduanLocalCount;// } |