0

I am working on an asp.net business application and using nhibernate. My objective of using nhibernate is to minimize/avoid application porting effort on different databases(Oracle, SQL Server, Postgres etc).

I have a scenario where i have to dynamically check the database table schema and build some functionality on it. First thing comes to my mind is that to create stored procedures and port it on different databases. My stored procedure looks like this...


SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = (
    SELECT MasterTableName FROM SystemDocument WHERE DocId = @vDocumentID
)

Now i have 2 questions here...

1- Is there any alternate in NHibernate to achieve this by avoiding stored procedures?

2- If answer of the first question is NO :( then how can i fill a dto/POCO that can contain the columns and their types of the table without mapping it with nhibernate?

I shall be very thankful for your suggestions on this.

Thanks, Asif

2 Answers 2

0

You can use regular sql and project that onto a dto object. I'd use a named query so it will be easier to change. I don't know if there is a way to auto map these named query based on the current dialect in use.

2
  • Thanks for the answer, but if i use a named query, it will not be portable for the databases other than sql server. Is there any workaround??
    – ak1
    Feb 22, 2010 at 15:05
  • Yeah that's the problem. I've never really dealt with this before, but I guess you'd need multiple versions of this query for each dialect. So multiple hbm.xml files with the same named-query just diffent implementations. Then you'd have to remember to use the right one depending on dialect.
    – dotjoe
    Feb 22, 2010 at 15:12
0

In C# you are able to achieve this in your Startup.cs file with the following code:

// Append database schema for update
 schemaHelper.AppendDatabaseUpdateSql(Assembly.GetExecutingAssembly(), sessionFactoryHelper.GetConfiguration(hibernateCfgPath));

// Append database schema for creation
schemaHelper.AppendDatabaseCreationSql(Assembly.GetExecutingAssembly(), sessionFactoryHelper.GetConfiguration(hibernateCfgPath));

These create the correct database tables from your mapping.

Otherwise see this post for another solution.

Hope that helped :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.