zoukankan      html  css  js  c++  java
  • Entity Framework Working with Transactions

    namespace TransactionsExamples 
    { 
        class TransactionsExample 
        { 
            static void StartOwnTransactionWithinContext() 
            { 
                using (var context = new BloggingContext()) 
                { 
                    using (var dbContextTransaction = context.Database.BeginTransaction()) 
                    { 
                        try 
                        { 
                            context.Database.ExecuteSqlCommand( 
                                @"UPDATE Blogs SET Rating = 5" + 
                                    " WHERE Name LIKE '%Entity Framework%'" 
                                ); 
     
                            var query = context.Posts.Where(p => p.Blog.Rating >= 5); 
                            foreach (var post in query) 
                            { 
                                post.Title += "[Cool Blog]"; 
                            } 
     
                            context.SaveChanges(); 
     
                            dbContextTransaction.Commit(); 
                        } 
                        catch (Exception) 
                        { 
                            dbContextTransaction.Rollback(); 
                        } 
                    } 
                } 
            } 
        } 
    }
    namespace TransactionsExamples 
    { 
         class TransactionsExample 
         { 
            static void UsingExternalTransaction() 
            { 
                using (var conn = new SqlConnection("...")) 
                { 
                   conn.Open(); 
     
                   using (var sqlTxn = conn.BeginTransaction(System.Data.IsolationLevel.Snapshot)) 
                   { 
                       try 
                       { 
                           var sqlCommand = new SqlCommand(); 
                           sqlCommand.Connection = conn; 
                           sqlCommand.Transaction = sqlTxn; 
                           sqlCommand.CommandText = 
                               @"UPDATE Blogs SET Rating = 5" + 
                                " WHERE Name LIKE '%Entity Framework%'"; 
                           sqlCommand.ExecuteNonQuery(); 
     
                           using (var context =  
                             new BloggingContext(conn, contextOwnsConnection: false)) 
                            { 
                                context.Database.UseTransaction(sqlTxn); 
     
                                var query =  context.Posts.Where(p => p.Blog.Rating >= 5); 
                                foreach (var post in query) 
                                { 
                                    post.Title += "[Cool Blog]"; 
                                } 
                               context.SaveChanges(); 
                            } 
     
                            sqlTxn.Commit(); 
                        } 
                        catch (Exception) 
                        { 
                            sqlTxn.Rollback(); 
                        } 
                    } 
                } 
            } 
        } 
    }
    namespace TransactionsExamples 
    { 
        class TransactionsExample 
        { 
            static void UsingTransactionScope() 
            { 
                using (var scope = new TransactionScope(TransactionScopeOption.Required)) 
                { 
                    using (var conn = new SqlConnection("...")) 
                    { 
                        conn.Open(); 
     
                        var sqlCommand = new SqlCommand(); 
                        sqlCommand.Connection = conn; 
                        sqlCommand.CommandText = 
                            @"UPDATE Blogs SET Rating = 5" + 
                                " WHERE Name LIKE '%Entity Framework%'"; 
                        sqlCommand.ExecuteNonQuery(); 
     
                        using (var context = 
                            new BloggingContext(conn, contextOwnsConnection: false)) 
                        { 
                            var query = context.Posts.Where(p => p.Blog.Rating > 5); 
                            foreach (var post in query) 
                            { 
                                post.Title += "[Cool Blog]"; 
                            } 
                            context.SaveChanges(); 
                        } 
                    } 
     
                    scope.Complete(); 
                } 
            } 
        } 
    }
  • 相关阅读:
    【剑指offer】数字在排序数组中出现的次数
    移动互联网的架构设计浅谈一
    Android开发中遇到的adb问题解决方法
    Datagrid分页、排序、删除代码
    新辰:关于个人站点安全问题的分析及对策探讨
    Android开发中,activity页面跳转后是空白
    实战——二、c#窗体(2)
    实战——一、c#窗体(1)
    c#的sealed修饰符
    c#中,类的重写中,new和override的区别
  • 原文地址:https://www.cnblogs.com/liandy0906/p/7136134.html
Copyright © 2011-2022 走看看