使用NHibernate执行DDL语句
如何通过 NHibernate 执行 DDL 语句?
需要明确的是,我不想从映射文件自动生成架构。我的 DDL 存储在纯文本文件中,格式如下:
CREATE TABLE Foo (Bar VARCHAR(10))
GO
CREATE TABLE Hello (World INTEGER)
GO
我想按顺序循环遍历这些文件并执行它们中的每一个。我可以打开一个 SqlConnection 并通过 SqlCommand 执行,但如果有一个好的方法来执行此操作,我想通过 NHibernate。这主要是因为我想尽可能保持与数据库无关:我现在有一个 SQL 数据库,但稍后我可能需要实现 Oracle 或 DB2...
我正在使用 .Net v3.51 和 NHibernate v2.1。我查看了 NHibernate SchemaExport 类,但找不到将其用于此目的的方法。
How can I execute a DDL statement via NHibernate?
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
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...
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我之前曾使用
session.Connection.CreateCommand
和session.Transaction.EnlistCommand
成功运行原始 SQL。这是我做过的类似事情的片段:
I've used
session.Connection.CreateCommand
andsession.Transaction.EnlistCommand
before with success to run raw SQL.Here's a snippet of something similar that I've done:
您可以从 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.