使用NHibernate执行DDL语句 [英] Using NHibernate to execute DDL statements
问题描述
如何通过NHibernate执行DDL语句?
How can I execute a DDL statement via NHibernate?
请明确说明,我不想要从映射文件中自动生成架构.我的DDL按照以下方式存储在纯文本文件中:
To be clear, I don't want to auto-generate my schema from my mapping files. My DDL is stored in plain text files along the lines of:
CREATE TABLE Foo (Bar VARCHAR(10))
GO
CREATE TABLE Hello (World INTEGER)
GO
我想依次遍历这些并执行它们中的每一个.我可以打开SqlConnection并通过SqlCommand执行,但是如果有一种不错的方法,我想通过NHibernate.这主要是因为我想保持数据库不可知性:我现在有一个SQL db,但是以后可能需要实现Oracle或DB2 ...
I want to cycle through these in order and execute each of them. I could just open a SqlConnection and execute via a SqlCommand but I'd like to go through NHibernate if there is a nice way to do this. This is mainly because I want to remain as database agnostic as possible: I have a SQL db now but I might need to implement Oracle or DB2 later...
我正在使用.Net v3.51和NHibernate v2.1.我查看了NHibernate SchemaExport类,但找不到为此目的使用此方法的方法.
I'm using .Net v3.51 and NHibernate v2.1. I looked at the NHibernate SchemaExport class but couldn't see a way to use this for this purpose.
推荐答案
您可以从ISession的Connection属性获取IDbConnection,但是您需要使用SqlCommand进行此操作.执行DDL超出了NHibernate的范围.
You can get an IDbConnection from an ISession's Connection property but you'll need to do this with SqlCommand. Executing DDL is outside of NHibernate's scope.
这篇关于使用NHibernate执行DDL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!