0

I have a query that due to performance reasons I need to craft the actual command using raw ADO.NET (it involves table-valued parameters). With LINQ to SQL or EF, I could simply pass the DbDataReader that was returned by DbCommand.ExecuteReader() to the DataContext.Translate<T>(DbDataReader) or ObjectContext.Translate<T>(DbDataReader) method and it would convert the result-set to objects, returning an IEnumerable<T>.

Is there any equivalent API in NHibernate that I can either pass a DbDataReader/IDataReader or even an DbCommand/IDbCommand and get NHibernate entities back?

Or perhaps there's a way to intercept the construction of the command created by ISession.CreateQuery(string) so that I could modify the underlying DbCommand to work the way I need to?

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93

1 Answers1

1

You can probably create a custom UserType to handle the table-valued parameter.

NHibernate does not expose the DataReader -> entities transformation in any public methods.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • It seems that this is a good place to start. `IUserType.NullSafeSet()` has a parameter of type `IDbCommand` which is what I need to add the table-value parameter. – Allon Guralnek Apr 07 '13 at 19:55
  • It was indeed the easiest way to make that work, thanks! I got to the point where I could do the following: `session.CreateSQLQuery("SELECT * FROM Customers WHERE Id IN (SELECT Value FROM :tableType)")`. But that requires calling `IQuery.AddEntity()` and such. I tried to move one abstraction level up by using HQL with the following: `session.CreateQuery("FROM Customers WHERE Id IN (SELECT Value FROM :tableType)")`, but that throws a `Antlr.Runtime.NoViableAltException`. I want to get to the point where I can use this from LINQ, but isn't HQL the next logical step? Maybe I need to skip HQL? – Allon Guralnek Apr 08 '13 at 13:33
  • I don't think you'll be able to move higher than SQL without deeper hacking. Just keep the SQL. – Diego Mijelshon Apr 08 '13 at 13:56