首页 > 数据库技术 > 详细

Entity Framework 的事务 DbTransaction

时间:2014-12-31 18:14:28      阅读:393      评论: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://www.cnblogs.com/xgao/p/4196168.html

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