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 进行数据迁移,否则基础数据是空的

  • 相关阅读:
    微信报警提示
    使用pygal图表显示网站API接口数据
    读写文本文件,乱码解决方案
    MD5加密
    将DataTable导入到SQL数据库表中
    NPOI组件操作Excel导入、导出
    二叉树由先序和中序建树
    用两个栈模拟队列
    math type白嫖教程
    IDEA常用快捷键
  • 原文地址:https://www.cnblogs.com/xcsn/p/6938055.html
Copyright © 2011-2022 走看看