开发者

alternate database schema queries in nhibernate

开发者 https://www.devze.com 2022-12-20 18:16 出处:网络
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, Postgr

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 sche开发者_C百科ma 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


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.


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 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号