熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

Entity Framework細節追蹤

2022-06-13   來源: .NET編程 

  為了加深對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;// }


From:http://tw.wingwit.com/Article/program/net/201311/13465.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.