如何使用 NHibernate(或 Fluent)检查表是否存在?
检查 NHibernate(或 Fluent-NHibernate)中是否存在表的最佳、最一致的方法是什么?
有可能吗?我的意思是,对于这样一个重型 ORM 来说,这似乎是一个简单的任务。
另外,关于一个相关的问题,您可以检查 NHibernate 是否存在一组表或整个模式吗?
Whats the best, most consistent way to check if a table exists in NHibernate (or with Fluent-NHibernate)?
Is it even possible? I mean it seems like a simple task for such a heavy-duty ORM.
Also on a related question, can you check if a set of tables or a whole schema exists with NHibernate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将 NHibernate 配置存储在某处或在构建会话工厂之前执行此操作,则可以根据数据库验证生成的架构。
If you store you NHibernate configuration somewhere or do it before you build your session factory it is possible to validate the generated schema against the database.
我查看了 SchemaUpdate 的源代码。我知道 SchemaUpdate 可以检测到丢失的表,然后生成创建脚本,而不是更新脚本。果然,答案就在那里。
如果数据库中不存在表,
NHibernate.Tool.hbm2ddl.DatabaseMetadata
对象中的GetTableMetadata
函数将返回 null。通常,SchemaUpdate 创建一个 DatabaseMetadata 对象并传入
Configuration
对象。但看起来创建 DatabaseMetadata 所需的只是 DBConnection 和 Dialect 对象。SchemaUpdate 因此创建一个 DatabaseMetadata:
NHibernate.Cfg.Configuration
然后调用I looked in the source code for SchemaUpdate. I knew SchemaUpdate could detect a missing table and then generate a create script, rather than an update script. Sure enough, the answer was in there.
The
GetTableMetadata
function inNHibernate.Tool.hbm2ddl.DatabaseMetadata
object will return null if a table does not exist in a database.Normally, SchemaUpdate creates a DatabaseMetadata object and passes in into a
Configuration
object. But it looks like all you need to create a DatabaseMetadata is a DBConnection and Dialect object.SchemaUpdate creates a DatabaseMetadata thusly:
NHibernate.Cfg.Configuration
then calls当搜索这样的解决方案时,这个问题和响应在谷歌中随处可见,所以我想我会以更精确和简洁的方式为我提供有效的内容[由于问题年龄,很可能是一个补充]; “IsTable”:
希望对某人有所帮助,因为在偶然发现这一点之前,我实施了类似于上述的复杂策略;)
This question and response popped up everywhere in google when searching for such a solution, so I thought I would put what worked [due to questions age, most likely an addition] for me in a more precise and succinct manner; "IsTable":
Hope that helps somebody because I implemented a convoluted strategy similar to the above before stumbling on this ;)