ef6中的事务和回滚

时间:2017-02-22 21:51:58

标签: asp.net-mvc entity-framework entity-framework-6

我目前在EF6和MVC5中有一个(2)步骤,以更新2个不同的表。更新第二个表取决于在第一个表更新后获取键值。如何在交易中完成此操作?这就是我目前正在做的事情。

 db.tblFollowUpSurveys.Add(Survey);
            try
                {
                db.SaveChanges();
                // using the ID parameter search the tblsurvey table and return the surveyid

                tblFollowUpSurvey baseFUPSurvey = new tblFollowUpSurvey();
                tblFollowUp baseFUPS = new tblFollowUp();
                tblSystemConstant baseSC = new tblSystemConstant();

                baseSC = (from s in db.tblSystemConstants
                          select s).First();

                baseFUPSurvey = (from a in db.tblFollowUpSurveys
                                 where a.ID == ID
                                 select a).First();

                baseFUPS = (from b in db.tblFollowUps
                                  where b.ID == ID && b.FollowUpYear == baseSC.FollowUpYear
                                  select b).First();

                if (baseFUPS != null)
                    {
                    baseFUPS.SurveyID = baseFUPSurvey.SurveyId;
                    baseFUPS.SurveyPSN = PSN;
                    baseFUPS.SurveyType = "SL";
                    baseFUPS.SurveyStatus = "SELF";
                    baseFUPS.ModifiedBy = "Self";
                    baseFUPS.ContactMethod = "S";
                    baseFUPS.SurveyDate = DateTime.Now;
                    baseFUPS.ModifiedDate = DateTime.Now;

                    try
                        {
                        db.SaveChanges();

                        return ("Record Saved");
                        }
                    catch
                        {
                        return ("Record not Saved");
                        }
                    }
                else
                    {
                    return ("No FollowUp record found");
                    }
                }
            catch
                {
                return ("Record not Saved");
                }

所以这就是我解决这个问题的方法

public string postSurveyResults(vmFollowUpSurvey model, int PSN, string ID)
{
using (MVCEntities context = new MVCEntities() )
    {
    using (var transaction = context.Database.BeginTransaction())
        {
        try
            {
            tblSurvey Survey = new tblSurvey();
                {
                code assignment
                };
            context.tblSurveys.Add(Survey);
            context.SaveChanges();

            tblUp baseFUPS = new tblUp();
            tblConstant baseSC = new tblConstant();
            baseSC = (from s in context.tblConstants
                      select s).First();

            baseFUPS = (from b in context.tblUps
                              where b.ID == ID && b.Year == baseSC.Year
                              select b).First();

                baseFUPS.ID = Survey.Id;
                code assignments


            context.SaveChanges();
            transaction.Commit();
            return "Record Saved";

            }
        catch (Exception ex)
            {
            transaction.Rollback();
            return ex.Message;
            }
        }
    }
}

0 个答案:

没有答案