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;
}