本文整理汇总了C#中Database.AddOrUpdate方法的典型用法代码示例。如果您正苦于以下问题:C# Database.AddOrUpdate方法的具体用法?C# Database.AddOrUpdate怎么用?C# Database.AddOrUpdate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Database
的用法示例。
在下文中一共展示了Database.AddOrUpdate方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DatabaseAddsUpdatesAndQueriesActiveTransaction
public void DatabaseAddsUpdatesAndQueriesActiveTransaction()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var objects = TestResourceFactory.GetMockClassAObjects(12);
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
db.Load();
db.BeginTransaction();
foreach (var obj in objects)
db.AddOrUpdate(obj, 0);
var update = db.Fetch(3);
update.Name = "Updated " + update.Id;
db.AddOrUpdate(update, update.Id);
db.FlushAll();
}
using (var db = new Database<int, MockClassA>(_testName + ".database"))
{
db.Load();
db.Update<MockClassA>(u => !u.Value<string>("Name").Contains("Updated"), m => m.Name = "batch " + m.Id);
db.FlushAll();
var old = db.Select(s => s.Value<string>("Name").Contains("Updated"));
Assert.AreEqual(1, old.Count);
Assert.AreEqual("Updated 3", old.Single().Name);
var updates = db.SelectFirst(s => s.Value<string>("Name").Contains("batch"), 11);
Assert.AreEqual(11, updates.Count);
Assert.AreEqual(1, updates.First().Id);
Assert.AreEqual(12, updates.Last().Id);
}
}
示例2: AddOrUpdateWithoutIdAddsWithIdZeroAndUpdatesWithNonZeroId
public void AddOrUpdateWithoutIdAddsWithIdZeroAndUpdatesWithNonZeroId()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
var objs = TestResourceFactory.GetMockClassAObjects(100).ToList();
using (var fLock = new ManagedFileLock(_testName))
{
Cleanup();
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
db.Load();
objs.ToList().ForEach(o => o.Id = db.AddOrUpdate(o));
db.FlushAll();
}
using (var db = new Database<int, MockClassA>(_testName + ".database"))
{
db.Load();
var last = db.Fetch(objs.Last().Id);
Assert.IsNotNull(last);
last.Name = "last";
db.AddOrUpdate(last);
db.FlushAll();
}
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
db.Load();
var last = db.Fetch(objs.Last().Id);
Assert.IsNotNull(last);
Assert.AreEqual("last", last.Name);
Assert.IsNotNull(db.Fetch(objs.First().Id));
db.FlushAll();
}
}
}
示例3: FullEnlistmentScopeCommitsTest
public void FullEnlistmentScopeCommitsTest()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
var objects = TestResourceFactory.GetMockClassAObjects(12);
ITransaction trans = null;
using (var fLock = new ManagedFileLock(_testName))
{
Cleanup();
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>(), new BinConverter32(), new BSONFormatter(),
new TransactionManager<int, MockClassA>() { DistributedScopeEnlistment = TransactionEnlistmentType.FullEnlistmentNotification }))
{
db.Load();
using (var scope = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew))
{
trans = db.BeginTransaction();
foreach (var obj in objects)
obj.Id = db.AddOrUpdate(obj, 0);
var update = db.Fetch(3);
update.Name = "Updated " + update.Id;
db.AddOrUpdate(update, update.Id);
db.Delete(objects.Last().Id);
scope.Complete();
}
while (!trans.IsComplete)
Thread.Sleep(100);
}
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>(), new BinConverter32(), new BSONFormatter(),
new TransactionManager<int, MockClassA>() { DistributedScopeEnlistment = TransactionEnlistmentType.FullEnlistmentNotification }))
{
db.Load();
using (var scope = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew))
{
trans = db.BeginTransaction();
db.Update<MockClassA>(u => !u.Value<string>("Name").Contains("Updated"), m => m.Name = "batch " + m.Id);
var old = db.Select(s => s.Value<string>("Name").Contains("Updated"));
Assert.AreEqual(1, old.Count);
Assert.AreEqual("Updated 3", old.Single().Name);
var updates = db.SelectFirst(s => s.Value<string>("Name").Contains("batch"), 11);
Assert.AreEqual(10, updates.Count);
Assert.AreEqual(1, updates.First().Id);
Assert.AreEqual(11, updates.Last().Id);
scope.Complete();
}
while (!trans.IsComplete)
Thread.Sleep(100);
}
}
}
示例4: SinglePhaseScopeRollsBackTest
public void SinglePhaseScopeRollsBackTest()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
var objects = TestResourceFactory.GetMockClassAObjects(12);
ITransaction trans = null;
using (var fLock = new ManagedFileLock(_testName))
{
Cleanup();
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>(), new BinConverter32(), new BSONFormatter(),
new TransactionManager<int, MockClassA>() { DistributedScopeEnlistment = TransactionEnlistmentType.SinglePhasePromotable }))
{
db.Load();
using (var scope = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew))
{
trans = db.BeginTransaction();
foreach (var obj in objects)
obj.Id = db.AddOrUpdate(obj, 0);
var update = db.Fetch(3);
update.Name = "Updated " + update.Id;
db.AddOrUpdate(update, update.Id);
db.Delete(objects.Last().Id);
}
while (!trans.IsComplete)
Thread.Sleep(100);
}
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>(), new BinConverter32(), new BSONFormatter(),
new TransactionManager<int, MockClassA>() { DistributedScopeEnlistment = TransactionEnlistmentType.SinglePhasePromotable }))
{
db.Load();
Assert.AreEqual(0, db.Length);
}
}
}
示例5: FullEnlistmentScopeRollsBackOnExceptionTest
public void FullEnlistmentScopeRollsBackOnExceptionTest()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
var objects = TestResourceFactory.GetMockClassAObjects(12);
ITransaction trans = null;
using (var fLock = new ManagedFileLock(_testName))
{
Cleanup();
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>(), new BinConverter32(), new BSONFormatter(),
new TransactionManager<int, MockClassA>() { DistributedScopeEnlistment = TransactionEnlistmentType.FullEnlistmentNotification }))
{
db.Load();
try
{
using (var scope = new System.Transactions.TransactionScope(TransactionScopeOption.RequiresNew))
{
using (trans = db.BeginTransaction())
{
foreach (var obj in objects)
obj.Id = db.AddOrUpdate(obj, 0);
var update = db.Fetch(3);
update.Name = "Updated " + update.Id;
db.AddOrUpdate(update, update.Id);
db.Delete(objects.Last().Id);
throw new Exception();
}
}
}
catch (Exception) { }
}
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>(), new BinConverter32(), new BSONFormatter(),
new TransactionManager<int, MockClassA>() { DistributedScopeEnlistment = TransactionEnlistmentType.FullEnlistmentNotification }))
{
db.Load();
Assert.AreEqual(0, db.Length);
}
}
}
示例6: TransactionFailsWhenUpdatingDeletedRecord
public void TransactionFailsWhenUpdatingDeletedRecord()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var objects = TestResourceFactory.GetMockClassAObjects(12);
using (var fLock = new ManagedFileLock(_testName + ".database"))
{
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
db.Load();
db.BeginTransaction();
foreach (var obj in objects)
db.AddOrUpdate(obj, 0);
var update = db.Fetch(3);
update.Name = "Updated " + update.Id;
db.AddOrUpdate(update, update.Id);
db.Flush();
}
using (var db = new Database<int, MockClassA>(_testName + ".database"))
{
db.Load();
db.BeginTransaction();
db.Delete(d => d.Value<string>("Name").Contains("Updated"));
db.Update<MockClassA>(u => u.Value<string>("Name").Contains("Updated"), m => m.Name = "Should Fail " + m.Id);
db.Flush();
}
}
}
示例7: DatabaseRebuildsWithNewSeedSize
public void DatabaseRebuildsWithNewSeedSize()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var objects = TestResourceFactory.GetMockClassAObjects(10000);
using (var fLock = new ManagedFileLock(_testName + ".database"))
{
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>() { InitialDbSize = 9000 }))
{
db.Load();
using (var tran = db.BeginTransaction())
{
foreach (var obj in objects)
obj.Id = db.AddOrUpdate(obj, 0);
var update = db.Fetch(3);
update.Name = "Updated " + update.Id;
db.AddOrUpdate(update, update.Id);
tran.Commit();
}
db.FlushAll();
}
var deletes = objects.Skip(5000).Take(2500);
var queryDeletes = objects.Skip(7500);
using (var db = new Database<int, MockClassA>(_testName + ".database"))
{
db.Load();
var check = db.Fetch(10000);
using (var tran = db.BeginTransaction())
{
deletes.ToList().ForEach(d => db.Delete(d.Id));
db.Delete(s => s.Value<int>("Id") >= objects.First().Id);
tran.Commit();
}
}
}
}