本文整理汇总了C#中Database.BeginTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# Database.BeginTransaction方法的具体用法?C# Database.BeginTransaction怎么用?C# Database.BeginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Database
的用法示例。
在下文中一共展示了Database.BeginTransaction方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: UpdateFromQuery
public void UpdateFromQuery()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var objs = TestResourceFactory.GetMockClassAObjects(100).ToList();
objs.OfType<MockClassC>().ToList().ForEach(o => o.ReferenceCode = null);
var first = objs.FirstOrDefault() as MockClassC;
var refCode = "R " + new Random().Next();
var select = new ScalarSelectExpression(
new string[] { "Location.Y" },
new CompareToken("Id", CompareEnum.Equals, 1),
new CompareToken("Location.X", CompareEnum.Equals, first.Location.X),
new CompareToken("Name", CompareEnum.Like, first.Name.Substring(1, first.Name.Length - 2)));
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
var eval = new ExpressionResolver<int, MockClassA>(db);
db.Load();
using (var t = db.BeginTransaction())
{
objs.ToList().ForEach(o => o.Id = db.Add(o));
t.Commit();
}
using (var t = db.BeginTransaction())
{
var count = eval.ExecuteUpdate(
new UpdateExpression(
typeof(MockClassC).AssemblyQualifiedName,
select,
new UpdateToken("Location.Y", 133.33, ValueEnum.Float),
new UpdateToken("Location.Z", 222.11, ValueEnum.Float),
new UpdateToken("ReferenceCode", refCode)));
Assert.AreEqual(1, count);
t.Commit();
var results = eval.ExecuteScaler(select);
Assert.AreEqual(1, results.Count);
Assert.AreEqual(133.33f, ((MockClassC)db.Fetch(1)).Location.Y);
Assert.AreEqual(222.11f, ((MockClassC)db.Fetch(1)).Location.Z);
Assert.AreEqual(refCode, ((MockClassC)db.Fetch(1)).ReferenceCode);
}
}
}
示例2: SelectExpressionReturnsFirstItems
public void SelectExpressionReturnsFirstItems()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var objs = TestResourceFactory.GetMockClassAObjects(100).ToList();
var max = objs.Cast<MockClassC>().Max(o => o.Location.X);
var min = objs.Cast<MockClassC>().Min(o => o.Location.X);
var first = objs.FirstOrDefault() as MockClassC;
var select = new WhereExpression(10, true, new CompareToken("Location.X", CompareEnum.Greater, max - min));
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
var eval = new ExpressionResolver<int, MockClassA>(db);
db.Load();
using (var t = db.BeginTransaction())
{
objs.ToList().ForEach(o => o.Id = db.Add(o));
t.Commit();
var results = eval.ExecuteSelect(select);
Assert.LessOrEqual(results.Count, 10);
foreach (var result in results)
MockClassC.Validate(result.ToObject<MockClassC>(), objs.FirstOrDefault(o => o.Id == result.Value<int>("Id")) as MockClassC);
}
}
}
示例3: CanCompactThenReadingWithValue
public void CanCompactThenReadingWithValue()
{
var database = new Database(new MemoryPersistentSource())
{
new Table(x => x.Value<string>("id"), "test")
};
var value = Encoding.UTF8.GetBytes(new string('$', 1024));
var count = 330;
for (int i = 0; i < count; i++)
{
database.BeginTransaction();
database.Tables[0].Put(new RavenJObject { { "id", i } }, value);
database.Commit();
}
for (int i = 0; i < count; i++)
{
var readResult = database.Tables[0].Read(new RavenJObject { { "id", i } });
Assert.Equal(value, readResult.Data());
}
database.Compact();
for (int i = 0; i < count; i++)
{
var readResult = database.Tables[0].Read(new RavenJObject { { "id", i } });
Assert.Equal(value, readResult.Data());
}
}
示例4: SelectExpressionReturnsItems
public void SelectExpressionReturnsItems()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var objs = TestResourceFactory.GetMockClassAObjects(100).ToList();
var first = objs.FirstOrDefault() as MockClassC;
var select = new WhereExpression(
new CompareToken("ReferenceCode", CompareEnum.Like, first.ReferenceCode),
new CompareToken("Location.X", CompareEnum.Equals, first.Location.X),
new CompareToken("Name", CompareEnum.Like, first.Name.Substring(1, first.Name.Length - 2)));
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
var eval = new ExpressionResolver<int, MockClassA>(db);
db.Load();
using (var t = db.BeginTransaction())
{
objs.ToList().ForEach(o => o.Id = db.Add(o));
t.Commit();
var results = eval.ExecuteSelect(select);
Assert.AreEqual(1, results.Count);
MockClassC.Validate(results[0].ToObject<MockClassC>(), objs.FirstOrDefault() as MockClassC);
}
}
}
示例5: DatabaseErrosWithetId
public void DatabaseErrosWithetId()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var core = new FileCore<int, long>(new Seed32(999));
var formatter = TestResourceFactory.CreateJsonFormatterWithoutArrayFormatting();
var stopWatch = new Stopwatch();
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", core))
{
db.Load();
using (var t = db.BeginTransaction())
{
var a = TestResourceFactory.CreateRandom();
a.Id = 5;
var obj = JObject.FromObject(a, formatter.Serializer);
db.AddJObj(obj);
}
}
}
示例6: TransactionsGetExplicitlySetIsolationLevel
public void TransactionsGetExplicitlySetIsolationLevel()
{
var adapter = new StubAdapterWithTransaction();
Database db = new Database(adapter);
db.BeginTransaction(IsolationLevel.Serializable);
Assert.AreEqual(IsolationLevel.Serializable, adapter.IsolationLevel);
}
示例7: OpenDictionary
protected void OpenDictionary()
{
PersistentSource = new FileBasedPersistentSource(tempPath, "test", writeThrough: true);
database = new Database(PersistentSource);
Table = database.Add(new Table("Test"));
database.Initialize();
database.BeginTransaction();
}
示例8: NamedTransactionsGetUnspecifiedIsolationLevel
public void NamedTransactionsGetUnspecifiedIsolationLevel()
{
var adapter = new StubAdapterWithTransaction();
Database db = new Database(adapter);
db.BeginTransaction("tran name");
Assert.AreEqual(IsolationLevel.Unspecified, adapter.IsolationLevel);
}
示例9: Transaction
public Transaction(Database db, IsolationLevel isolationLevel)
{
_db = db;
_db.BeginTransaction(isolationLevel);
if (_db.BaseTransaction == null)
{
_db.BaseTransaction = this;
}
}
示例10: CryptoCommitsLargeTransactions
public void CryptoCommitsLargeTransactions()
{
_testName = System.Reflection.MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
for (var i = 0; i <= 25; i++)
{
_key.AppendChar((char)(((i + 25763) * i * Math.PI) % char.MaxValue));
_vec.AppendChar((char)(((i + 41359) * i * Math.PI) % char.MaxValue));
}
var objects = new List<ResourceContainer>()
{
new Mocks.MockImageContainer(testRes.Luna_DIFF) { Name = "Luna_DIFF"},
new Mocks.MockImageContainer(testRes.Luna_MAT) { Name = "Luna_MAT"},
new Mocks.MockImageContainer(testRes.Luna_NRM) { Name = "Luna_NRM"}
};
using (var db = new Database<string, ResourceContainer>
(_testName + ".database", "Name", new FileCore<string, long>(new SeedString(255), new Seed64()),
new BinConverterString(),
new QueryCryptoFormatter(new RC2Crypto(_vec), new BSONFormatter(), _key)))
{
db.Load();
using (var t = db.BeginTransaction())
{
foreach (var o in objects)
o.Name = db.Add(o);
t.Commit();
}
using (var t = db.BeginTransaction())
{
foreach (var o in objects)
db.Update(o, o.Name);
t.Commit();
}
}
}
示例11: CanCompactWhenDataHasNoValue
public void CanCompactWhenDataHasNoValue()
{
var database = new Database(new MemoryPersistentSource())
{
new Table(x => x.Value<string>("id"), "test")
};
database.BeginTransaction();
database.Tables[0].UpdateKey(new JObject { { "id", 1 }, { "name", "ayende" } });
database.Commit();
database.BeginTransaction();
database.Tables[0].UpdateKey(new JObject { { "id", 1 }, { "name", "oren" } });
database.Commit();
database.Compact();
}
示例12: OpenDictionary
protected void OpenDictionary()
{
persistentSource = new FileBasedPersistentSource(Path.GetTempPath(), "test_", writeThrough: false);
Database = new Database(persistentSource);
tableOne = Database.Add(new Table("Test1"));
tableTwo = Database.Add(new Table("Test2"));
Database.Initialze();
Database.BeginTransaction();
}
示例13: ResetDatabase
protected override void ResetDatabase()
{
using (var database = new Database("SQLiteTest"))
{
database.BeginTransaction();
database.Execute(new Sql("DELETE FROM Entity"));
database.Execute(new Sql("DELETE FROM sqlite_sequence WHERE name = @0", "Entity"));
database.CompleteTransaction();
}
}
示例14: DatabaseDeletesFirstLastWithActiveTransactions
public void DatabaseDeletesFirstLastWithActiveTransactions()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
var objs = TestResourceFactory.GetMockClassAObjects(100).ToList();
var ids = new List<int>();
using (var fLock = new ManagedFileLock(_testName))
{
Cleanup();
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id", new FileCore<int, long>(), new BinConverter32(), new JSONFormatter()))
{
db.Load();
var t = db.BeginTransaction();
objs.ToList().ForEach(o => ids.Add(db.Add(o)));
var last = db.SelectLast(s => true, 1).LastOrDefault();
Assert.IsNotNull(last);
var count = db.Update(s => s.Value<string>("Name") == last.Name
, new System.Action<MockClassA>(a => a.Name = "last"));
Assert.AreEqual(1, count);
var selected = db.Select(s => true);
Assert.AreEqual(100, selected.Count);
count = db.DeleteFirst(s => true, 10);
Assert.AreEqual(10, count);
selected = db.Select(s => true);
Assert.AreEqual(90, selected.Count);
count = db.DeleteLast(s => true, 10);
Assert.AreEqual(10, count);
selected = db.Select(s => true);
Assert.AreEqual(80, selected.Count);
t.Rollback();
}
}
}
示例15: DeletesFirst10FromQuery
public void DeletesFirst10FromQuery()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var objs = TestResourceFactory.GetMockClassAObjects(100).ToList();
var first = objs.FirstOrDefault() as MockClassC;
var delete = new DeleteExpression(99, true,
new CompareToken("Name", CompareEnum.GreaterOrEqual, first.Name));
using (var db = new Database<int, MockClassA>(_testName + ".database", "Id"))
{
var eval = new ExpressionResolver<int, MockClassA>(db);
db.Load();
using (var t = db.BeginTransaction())
{
objs.ToList().ForEach(o => o.Id = db.Add(o));
t.Commit();
}
using (var t = db.BeginTransaction())
{
var results = eval.ExecuteDelete(delete);
Assert.LessOrEqual(1, results);
t.Commit();
Assert.IsNull(db.Fetch(first.Id));
}
Assert.IsNull(db.Fetch(first.Id));
}
}