/// <summary>
        /// Flushes this instance.
        /// </summary>
        public void Flush()
        {
            if (CurrentTargetTable == null)
            {
                return;
            }

            try
            {
                //using (var connection = RequestConnection())
                using (var connection = new SqlConnection(ConfigService.ConnectionSettings.ConnectionString))
                {
                    connection.Open();
                    using (var bcp = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, null))
                    //using (var bcp = new SqlBulkCopy(connection))
                    {
                        bcp.BatchSize            = CurrentTargetTable.Rows.Count < 1000 ? CurrentTargetTable.Rows.Count : BatchSize;
                        bcp.BulkCopyTimeout      = Timeout;
                        bcp.DestinationTableName = TargetTableName;
                        foreach (var mapping in ColumnMappings.OfType <SqlBulkCopyColumnMapping>())
                        {
                            bcp.ColumnMappings.Add(mapping);
                        }
                        bcp.WriteToServer(CurrentTargetTable);
                    }
                    connection.Close();
                }
            }
            finally
            {
                CurrentTargetTable = TargetTableSource.Clone();
            }
        }
        /// <summary>
        /// Inserts the specified instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        public void Insert(T instance)
        {
            if (instance == null)
            {
                return;
            }

            if (Mapper == null)
            {
                Mapper = instance.CreateBulkInsertMapper(TargetTableSource, instance, Target);
            }

            if (CurrentTargetTable == null)
            {
                CurrentTargetTable = TargetTableSource.Clone();
            }

            //if (instance is ICDCodedTarget)
            {
                instance.CleanBeforeSave();
            }

            var row = CurrentTargetTable.NewRow();

            row.ItemArray = Mapper.InstanceValues(instance).ToArray();

            var datasetRecord = instance as DatasetRecord;

            if (datasetRecord != null && (row[typeof(Dataset).Name + "_Id"] == DBNull.Value || row[typeof(Dataset).Name + "_Id"] == null))
            {
                row[typeof(Dataset).Name + "_Id"] = datasetRecord.Dataset.Id;
            }

            CurrentTargetTable.Rows.Add(row);

            if (CurrentTargetTable.Rows.Count == BatchSize)
            {
                Flush();
            }
        }
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    try
                    {
                        Flush();

                        //Task.Factory.StartNew(() =>
                        //{
                        //    var configService = ServiceLocator.Current.GetInstance<IConfigurationService>();
                        //    var dbCreator = ServiceLocator.Current.GetInstance<IDatabaseCreator>(CreatorNames.Sql);
                        //    dbCreator.TruncateDbLogFile(configService.ConnectionSettings.ConnectionString);
                        //}, TaskCreationOptions.LongRunning);
                    }
                    finally
                    {
                        try
                        {
                            if (CurrentTargetTable != null)
                            {
                                CurrentTargetTable.Dispose();
                                CurrentTargetTable = null;
                            }
                        }
                        finally
                        {
                            if (TargetTableSource != null)
                            {
                                TargetTableSource.Dispose();
                                TargetTableSource = null;
                            }
                        }
                    }
                }
                _disposed = true;
            }
        }