-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
63 lines (53 loc) · 1.4 KB
/
Copy pathProgram.cs
File metadata and controls
63 lines (53 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Aspects;
using LinqToDB;
using LinqToDB.Async;
using LinqToDB.Data;
using LinqToDB.Mapping;
namespace TransactionAspect
{
static class Program
{
[Table(Name="Customers")]
public sealed class Customer
{
[PrimaryKey, Identity] public int CustomerID = default;
[Column, NotNull] public string CompanyName = default!;
}
static readonly DataOptions _options = new DataOptions().UseSQLite("Data Source=TestDatabase.sqlite");
static void Main()
{
using var db = new DataConnection(_options);
db
.CreateTable<Customer>(tableOptions : TableOptions.CheckExistence)
.BulkCopy(
[
new() { CompanyName = "Company 1" },
new() { CompanyName = "Company 2" }
]);
PrintList(GetCustomers(db));
PrintList(GetCustomersAsync(db).Result);
static void PrintList(List<Customer> list)
{
foreach (var customer in list)
{
Console.WriteLine($"{customer.CustomerID} : {customer.CompanyName}");
}
}
}
[Transaction(IsolationLevel = IsolationLevel.ReadUncommitted)]
public static List<Customer> GetCustomers(DataConnection db)
{
return db.GetTable<Customer>().ToList();
}
[Transaction]
public static Task<List<Customer>> GetCustomersAsync(DataConnection db)
{
return db.GetTable<Customer>().ToListAsync();
}
}
}