# EF事务
//方式一:
//repository.DbContextBeginTransaction或 SellOrderRepository.Instance.DbContextBeginTransaction
WebResponseContent webResponse= repository.DbContextBeginTransaction(() =>
{
//如果想要回滚,返回new WebResponseContent().Error("返回消息")
return new WebResponseContent().OK();
});
//判断事务是否执行成功
if (webResponse.Status)
{
}
//方式二:
using (var transaction = repository.DbContext.Database.BeginTransaction())
{
try
{
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
transaction.Rollback();
}
}
//方式三:
using (var transaction = DBServerProvider.DbContext.Database.BeginTransaction())
{
try
{
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
transaction.Rollback();
}
}
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
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