示例#1
0
        internal static void Update(this DbConnection connection, object data, string tableName,
                                    string whereExpression                 = null,
                                    IFieldInfoCollection fields            = null, OnConflictOption option        = OnConflictOption.Default,
                                    DbTransaction transaction              = null, ICommandBuilder commandBuilder = null,
                                    IExceptionProcessor exceptionProcessor = null, object keyValue                = null)
        {
            if (data is null)
            {
                throw new ArgumentNullException("data");
            }

            commandBuilder = commandBuilder ?? DefaultCommandBuilder;

            if (data is IPersistanceTracking)
            {
                ((IPersistanceTracking)data).OnUpdating();
            }

            using (var command = connection.CreateCommand())
            {
                commandBuilder.BuildUpdate(command,
                                           data,
                                           tableName,
                                           fields,
                                           whereExpression: whereExpression,
                                           option: option,
                                           keyValue: keyValue);

                try
                {
                    var changes = ExecuteNonQuery(connection, command, transaction);
                    if (option != OnConflictOption.Ignore)
                    {
                        Debug.Assert(changes > 0, "update command resulted in no changes");
                    }
                }
                catch (Exception e)
                {
                    if (exceptionProcessor != null)
                    {
                        throw exceptionProcessor.ProcessException(e, connection, command.CommandText, transaction);
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (data is IPersistanceTracking)
            {
                ((IPersistanceTracking)data).OnUpdated();
            }
        }