zoukankan      html  css  js  c++  java
  • 10.事务

    ef中,savechanges() 默认使用事务.

    using (var context = new BookStore())
    {
        context.Database.Log = Console.WriteLine;
        Author author1 = new Author() {    Name = "Mark" };
        Author author2 = new Author() {    Name = "John" };
            
        context.Authors.Add(author1);
        context.SaveChanges();
        
        context.Authors.Add(author2);
        context.SaveChanges();
    }

    可以看到一个事务把两个insert包起来

    Opened connection at 10/29/2018 9:18:26 AM +00:00
    Started transaction at 10/29/2018 9:18:26 AM +00:00
    
    INSERT [dbo].[Authors]([Name])
    VALUES (@0)
    SELECT [AuthorId]
    FROM [dbo].[Authors]
    WHERE @@ROWCOUNT > 0 AND [AuthorId] = scope_identity()
    
    -- @0: 'Mark' (Type = String, Size = -1)
    -- Executing at 10/29/2018 9:18:26 AM +00:00
    -- Completed in 3 ms with result: SqlDataReader
    
    Committed transaction at 10/29/2018 9:18:26 AM +00:00
    Closed connection at 10/29/2018 9:18:26 AM +00:00
    
    Opened connection at 10/29/2018 9:18:26 AM +00:00
    Started transaction at 10/29/2018 9:18:26 AM +00:00
    
    INSERT [dbo].[Authors]([Name])
    VALUES (@0)
    SELECT [AuthorId]
    FROM [dbo].[Authors]
    WHERE @@ROWCOUNT > 0 AND [AuthorId] = scope_identity()
    
    -- @0: 'John' (Type = String, Size = -1)
    -- Executing at 10/29/2018 9:18:26 AM +00:00
    -- Completed in 1 ms with result: SqlDataReader
    
    Committed transaction at 10/29/2018 9:18:26 AM +00:00
    Closed connection at 10/29/2018 9:18:26 AM +00:00

    如果你想在一个事务中多次执行savechanges  你应该这样写

    using (var context = new BookStore())
    {
        context.Database.Log = Console.WriteLine;
        using (DbContextTransaction transaction = context.Database.BeginTransaction())
        {
            try
            {
                Author author1 = new Author() {    Name = "Mark" };
                Author author2 = new Author() {    Name = "John" };
                
                context.Authors.Add(author1);
                context.SaveChanges();
                
                context.Authors.Add(author2);
                context.SaveChanges();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
    }
    Opened connection at 10/29/2018 9:21:20 AM +00:00
    Started transaction at 10/29/2018 9:21:20 AM +00:00
    
    INSERT [dbo].[Authors]([Name])
    VALUES (@0)
    SELECT [AuthorId]
    FROM [dbo].[Authors]
    WHERE @@ROWCOUNT > 0 AND [AuthorId] = scope_identity()
    
    -- @0: 'Mark' (Type = String, Size = -1)
    -- Executing at 10/29/2018 9:21:20 AM +00:00
    -- Completed in 3 ms with result: SqlDataReader
    
    INSERT [dbo].[Authors]([Name])
    VALUES (@0)
    SELECT [AuthorId]
    FROM [dbo].[Authors]
    WHERE @@ROWCOUNT > 0 AND [AuthorId] = scope_identity()
    
    -- @0: 'John' (Type = String, Size = -1)
    -- Executing at 10/29/2018 9:21:20 AM +00:00
    -- Completed in 0 ms with result: SqlDataReader
    
    Committed transaction at 10/29/2018 9:21:20 AM +00:00
    Closed connection at 10/29/2018 9:21:20 AM +00:00
  • 相关阅读:
    txtexcelcvsxml存储测试数据
    webstorm 格式化代码(CTR+ALT+L)快捷键失效?
    解决jQuery触发dbclick事件同时也执行click事件
    css经典布局——头尾固定高度中间高度自适应布局
    js 如何访问跨域的iframe的元素
    获取textarea文本框所选字符光标位置索引,以及选中的文本值;textarea高度自适应,随着内容增加高度增加;获取输入框中的光标位置
    js 如何计算当年清明节日期
    验证插件使用笔记
    node 升级之后 执行gulp报错解决方案
    scss css管理相关
  • 原文地址:https://www.cnblogs.com/nocanstillbb/p/11495042.html
Copyright © 2011-2022 走看看