asp.net 使用事务机制

  using (System.Data.Common.DbTransaction traction = context.Connection.BeginTransaction())
                {
                    try
                    {
                        SRC_DEF_BASELINE_ALART t = new SRC_DEF_BASELINE_ALART();
                        //新添加的SRC_DEF_BASELINE_ALART的ID位已有的加1
                        short i;
                        if (context.SRC_DEF_BASELINE_ALART.Count() > 0)
                        {
                            i = context.SRC_DEF_BASELINE_ALART.Max(p => p.ALERT_ID);
                        }
                        else
                        {
                            i = 0;
                        }
                        t.ALERT_ID = (short)(i + 1);
                        t.ALERT_NAME = "lx";
                        t.DEFINE_PERSON = "zlf";
                        t.DEFINE_TIME = DateTime.Now;
                        t.PROGRESS_LEVEL_1 = short.Parse(TextBoxjdpc1.Text);
                        t.PROGRESS_LEVEL_2 = short.Parse(TextBoxjdpc2.Text);
                        t.PROGRESS_LEVEL_3 = short.Parse(TextBoxjdpc3.Text);
                        t.PROCEDURE_LEVEL_1 = short.Parse(TextBoxgypc1.Text);
                        t.PROCEDURE_LEVEL_2 = short.Parse(TextBoxgypc2.Text);
                        t.PROCEDURE_LEVEL_3 = short.Parse(TextBoxgypc3.Text);
                        t.TEMP_WORK_LEVEL_1 = short.Parse(TextBoxlspc1.Text);
                        t.TEMP_WORK_LEVEL_2 = short.Parse(TextBoxlspc2.Text);
                        t.TEMP_WORK_LEVEL_3 = short.Parse(TextBoxlspc3.Text);
                        t.QUESTION_LIST_LEVEL_1 = short.Parse(TextBoxjswt1.Text);
                        t.QUESTION_LIST_LEVEL_2 = short.Parse(TextBoxjswt2.Text);
                        t.QUESTION_LIST_LEVEL_3 = short.Parse(TextBoxjswt3.Text);
                        t.PROGRESS_DEV_ALERT_METHOD = dropdownProgress.SelectedItem.ToString();
                        context.CreateObjectSet<SRC_DEF_BASELINE_ALART>().AddObject(t);
                        //应用到平台
                        foreach (ListItem item in cblPlatform.Items)
                        {
                            if (item.Selected)
                            {
                                SRC_DEF_PLATFORM plat = (from p in context.SRC_DEF_PLATFORM
                                                         where p.PLATFORM_NAME == item.Text
                                                         select p).Single();
                                if (plat.ALERT_ID != null)
                                {
                                    if (MessageBox.Show("平台:'"+plat.PLATFORM_NAME + "'已经分配到预警X中,是否执行新的预警设置?", "确认信息:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                                    {
                                        plat.ALERT_ID = t.ALERT_ID;
                                    }
                                }
                                else
                                {
                                    plat.ALERT_ID = t.ALERT_ID;
                                }
                            }
                        }
                        context.SaveChanges(SaveOptions.DetectChangesBeforeSave);
                        traction.Commit();
                        Response.Redirect("mainAlerts.aspx");
                    }
                    catch (Exception ex)
                    {
                        traction.Rollback();
                        throw ex;
                    }
原文地址:https://www.cnblogs.com/liuxinls/p/3075913.html