码迷,mamicode.com
首页 > 其他好文 > 详细

程序后台写事务

时间:2015-10-10 20:01:34      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

1.db.Database.BeginTransaction();

 

2.

public Result AddActivityProduct(ActivityRuleProductViewModel model)
{
Result result = new Result();
using (var tran = this.BeginTransaction())
{
try
{
var rule = CreatRule(model);
var ruled = db.Set<Shop_ActivityRuleProduct>().Add(rule);

var ladders = CreateLadders(model, rule);
var laddereds = db.Set<Shop_ActivityRuleLadder>().AddRange(ladders);

if (model.Ladder.All(item => item.Gifts != null && item.Gifts.Any()))
{
var gifts = CreateGifts(model.Ladder);
var gifted = db.Set<Shop_ActivityPresentProduct>().AddRange(gifts);
}

if (model.ActivityProduct != null && model.ActivityProduct.Any())
{
var products = CreateActivityProduct(model, ruled);
var producted = db.Set<Shop_ActivityParticipationProduct>().AddRange(products);
}

result.IsSucceed = db.SaveChanges() > 0;
tran.Commit();
}
catch (DbEntityValidationException ex)
{
tran.Rollback();
result.IsSucceed = false;
result.Message = ex.Message;
}
catch (Exception ex)
{
tran.Rollback();
result.IsSucceed = false;
result.Message = ex.Message;
}
}
return result;
}

程序后台写事务

标签:

原文地址:http://www.cnblogs.com/fansb/p/4868061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!