public void CopyValue() { var data1 = new TestDB.User() { Birthday = DateTime.Now, IntColumn1 = 3, Name = "test" }; var data2 = new TestDB.User(); data1.CopyValueTo(data2, false); if (data1.Birthday != data2.Birthday || data1.IntColumn1 != data2.IntColumn1 || data1.Name != data2.Name) { throw new Exception("error"); } }
public void 乐观锁() { using (var db = new TestDB.DB.Test(ConStr, DatabaseType.SqlServer)) { db.Delete(db.User.Where(m => m.Name.StartsWith("lock_test"))); for (int i = 0; i < 50; i++) { var user = new TestDB.User { Name = "lock_test", IntColumn1 = 0 }; db.Insert(user); } var count = db.User.Where(m => m.Name.StartsWith("lock_test") && m.IntColumn1 == 0).Count(); if (count < 50) { throw new Exception("数量不对"); } } int unSuccessCount = 0; int SuccessCount = 0; //并发 Parallel.For(0, 300, (index) => { using (var db = new TestDB.DB.Test(ConStr, DatabaseType.SqlServer)) { while (true) { var user = db.User.FirstOrDefault(m => m.Name.StartsWith("lock_test") && m.IntColumn1 == 0); if (user == null) { Interlocked.Increment(ref unSuccessCount); return; } user.IntColumn1 = index + 1; if (db.Update(user, m => m.IntColumn1 == 0 && m.id == user.id) > 0) { Interlocked.Increment(ref SuccessCount); return; } } } }); using (var db = new TestDB.DB.Test(ConStr, DatabaseType.SqlServer)) { var count = db.User.Where(m => m.Name.StartsWith("lock_test") && m.IntColumn1 > 0).Count(); if (count < 50) { throw new Exception("数量不对"); } count = (from m in db.User group m by m.IntColumn1 into g select g.Key).Count(); if (count < 50) { throw new Exception("数量不对"); } } if (SuccessCount != 50) { throw new Exception("成功数量不对"); } if (unSuccessCount != 250) { throw new Exception("失败数量不对"); } }