zoukankan      html  css  js  c++  java
  • EntityFramework之事务

    一、EF事务

    引用程序集

    using System.Transactions;

    用法 

    var writer = new System.IO.StringWriter();
                try
                {
                    using (var ts = new TransactionScope())
                    {
                        using (var db = new EfContext())
                        { //TODO
         
                        }
                        ts.Complete();
                    }
                }
                catch (TransactionAbortedException ex)
                {
                    writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
                }
                catch (ApplicationException ex)
                {
                    writer.WriteLine("ApplicationException Message: {0}", ex.Message);
                }    

     参考文章:https://www.cnblogs.com/CreateMyself/p/4787856.html

      

    DBContext与SqlClient

      可用于ef和ado.net(SqlBulkCopy)的事务统一处理,_context为DbContext

    var dbConnection = (SqlConnection)_context.Database.Connection;
    
    var trans = (SqlTransaction)_context.Database.CurrentTransaction.UnderlyingTransaction;

      

    二、常见问题

    1.在多语句事务内不允许使用 CREATE DATABASE 语句

    一、问题

    使用ef codefirst开发,无法创建数据库的问题,如下提示

    Server Error in '/' Application.
    
    在多语句事务内不允许使用 CREATE DATABASE 语句。
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Data.SqlClient.SqlException: 在多语句事务内不允许使用 CREATE DATABASE 语句。
    
    Source Error: 
    
    
    Line 32:          */
    Line 33:         public ZmBlogDbContext(string nameOrConnectionString)
    Line 34:             : base(nameOrConnectionString)
    Line 35:         {
    Line 36: 

    二、解决方法
    这是 EF 抛出的异常,只要先创建好数据库就可以了

    create database [ZmBlog]

    提示:

    这个错误是在第一次运行abp出现的,创建数据库后,这时要使用 update-database 进行数据迁移,否则基础数据是空的

  • 相关阅读:
    Leetcode 12. Integer to Roman
    Leetcode 133. Clone Graph
    Leetcode 199. Binary Tree Right Side View
    Leetcode 200. Number of Islands
    React通过Ajax获取数据
    canvas鼠标点击划线
    制作图片墙
    CSS制作翻牌特效
    输入框制作方法
    初来咋到
  • 原文地址:https://www.cnblogs.com/xcsn/p/6938055.html
Copyright © 2011-2022 走看看