首页 > 数据库技术 > 详细

Entity Framework 的事务 DbTransaction

时间:2017-05-08 14:07:52      阅读:331      评论:0      收藏:0      [点我收藏+]

技术分享

public static void Transaction()
{
    myitEntities entity = null;
    DbTransaction tran = null;    try
    {
    entity = new myitEntities();
    entity.Connection.Open();
    tran = entity.Connection.BeginTransaction();
    Student st = entity.Student.FirstOrDefault(c => c.StudentID == 20);
    st.StudentName = "test";
    st.Age = 55;
    entity.SaveChanges();    // 提交事务
    tran.Commit();

    }    catch (Exception ex)
    {    if (tran != null)
    {        // 事务回滚
        tran.Rollback();
        Console.WriteLine("事务回滚");        throw ex;
    }
    }    finally {    if (entity != null && entity.Connection.State != ConnectionState.Closed)
    {
        entity.Connection.Close();
    }
    }
}

技术分享


Entity Framework 的事务 DbTransaction

原文:http://12883930.blog.51cto.com/12873930/1923194

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!