Pedro Galinatti
Pedro Galinatti

Reputation: 87

Delete all rows from table in NHibernate

I have an application which use Fluent NHibernate and NHibernate, I have an Generic DAO class, in this class I have generic methods to persist the data, but I want to create an generic method to delete all records from one table. At this moment I have this method:

Public Sub ClearTable(ByVal sTable As String)
    Using session = SessaoNHibernate.OpenSession()
        Using transaction = session.BeginTransaction()
            Try
                session.CreateSQLQuery("delete from " & sTable).ExecuteUpdate()
                transaction.Commit()
                session.Flush()
            Catch ex As Exception
                transaction.Rollback()
            End Try
        End Using
    End Using
End Sub

I have a generic class, so I want to create a method that don't need to pass the table name. Because the generic class knows the his type.

Upvotes: 2

Views: 3209

Answers (1)

Oskar Berggren
Oskar Berggren

Reputation: 5629

Just don't run it as an SQL query, and you can pass the class name instead:

session.CreateQuery("delete from EntityClass").ExecuteUpdate()

Upvotes: 5

Related Questions