欢迎光临

木子屋

标题: Entity Framework使用事务示例

作者: dnawo

日期: 2013-01-17 09:34 AM

分类: Web编程

内容: 复制内容到剪贴板 程序代码//using System.Transactions;
try
{
using (testContext context = new testContext())
{
using (TransactionScope trans = new TransactionScope())
{
//1.扣款
Account account = context.Accounts.Where(a => a.Id == 1).FirstOrDefault();
account.Money -= 100;
context.SaveChanges();
//throw new Exception("error");
//2.提现
Withdraw withdraw = new Withdraw()
{
Aid = account.Id,
Money = 100
};
context.Withdraws.Add(withdraw);
context.SaveChanges();
//3.提交
trans.Complete();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.805 ms

返回

标题: Entity Framework使用事务示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.805 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 0.836 ms

返回