my Audit table is not an LLBLGen Entity. I did find a way to do this if there is a transaction open.
I can get (me/this) dataaccessadapter.PhysicalTransaction and dataaccessadapter.PhysicalTransaction.Connection
like this
Public Sub InsertAuditRecord(ByVal EntityName As String, ByVal EntityID As String, ByVal EventName As String, ByVal EventDetails As String)
Dim transaction As SqlTransaction
Dim connection As SqlConnection
Dim isConnectionCreatedByMe As Boolean = False
If Me.PhysicalTransaction Is Nothing Then
isConnectionCreatedByMe = True
connection = New SqlConnection(Me.ConnectionString)
connection.Open()
transaction = Nothing
Else
isConnectionCreatedByMe = False
connection = CType(Me.PhysicalTransaction.Connection, SqlConnection)
transaction = CType(Me.PhysicalTransaction, SqlTransaction)
End If
Dim cmd As New System.Data.SqlClient.SqlCommand("Insert Into tblAudit(EntityName, EntityID, EventName, EventDetails, UserName) VALUES(@EntityName, @EntityID, @EventName, @EventDetails, @UserName)", connection, transaction)
Try
cmd.Transaction = CType(Me.PhysicalTransaction, SqlTransaction)
cmd.Parameters.Add(New SqlParameter("@EntityName", EntityName))
cmd.Parameters.Add(New SqlParameter("@EntityID", EntityID))
cmd.Parameters.Add(New SqlParameter("@EventName", EventName))
cmd.Parameters.Add(New SqlParameter("@EventDetails", EventDetails))
cmd.Parameters.Add(New SqlParameter("@UserName", Labelmaster.Data.DataApplication.Globals.CurrentUserName))
cmd.ExecuteNonQuery()
Catch ex As Exception
If Not transaction Is Nothing Then
cmd.Transaction.Rollback()
End If
Throw New ApplicationException("Could not create audit message for " & EntityName & ". See inner exception for more details", ex)
Finally
If isConnectionCreatedByMe Then
connection.Close()
End If
End Try
End Sub
it appears that OnSaveEntityComplete is within a transaction, but OnDeleteEntityComplete isn't. I'm sure this could be something i am doing when calling save or delete.