Using ARITHABORT with LLBLGen

I have recently been developing an application that uses LLBLGen with a vendor product SQL Server database. When attempting to add an entity, I was presented with the following exception:

SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException was unhandled
  Message="An exception was caught during the execution of an action query: INSERT failed because the following SET options have incorrect settings: ‘ARITHABORT’. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or query notifications and/or xml data type methods.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception."
  Source="SD.LLBLGen.Pro.ORMSupportClasses.NET20"

To date I had only used LLBLGen with Oracle databases so this exception had me a little confused to start with. After a little research it became clear the easy option is to turn on this option for the database as a whole, as shown below in the screenshot from SQL Server Management Studio:

SQL Server Management Studio : Database Options

However, I did not really want to go changing options in a vendor products database as I do not know what effect this could have on the actual product client code.

LLBLGen provides a method to set this value. If using the adapter method, you can achieve this by calling DataAccessAdapter.SetArithAbortFlag(true); This will then wrap each dynamically generated query with SET ARITHABORT ON; … SET ARITHABORT OFF; When I tried this, it had no effect; I could see that the generated dynamic SQL clearly contained the instructions to set ARITHABORT on and off, but still got the same exception.

Further investigation seemed to suggest that you may need to set or clear this flag in a separate batch to the query in which you need it enabled or disabled, which the SetArithAbortFlag technique does not do. Eventually, I came across this thread on the LLBLGen forum.

Based on that article, I came up with the following implementation of OpenConnection() which has solved the issue for me, hopefully it will for somebody else out there too.

internal class REDataAccessAdapter : DataAccessAdapter
{
    #region Overrides of DataAccessAdapterBase

    /// <summary>
    /// Opens the active connection object. If the connection is already open, nothing is done.
    ///             If no connection object is present, a new one is created. This has been
    ///             overridden in order to execute a SET ARITHABORT ON before any dynamic SQL
    ///             is executed.
    /// </summary>
    public override void OpenConnection()
    {
        base.OpenConnection();

        var connection = GetActiveConnection();
        var command = connection.CreateCommand();
        command.CommandText = "SET ARITHABORT ON";
        var arithAbortQuery = new ActionQuery(connection, command);
        WireTransaction(arithAbortQuery);
        arithAbortQuery.Execute();
    }

    #endregion
}
原文地址:https://www.cnblogs.com/JamesLi2015/p/2976777.html