使用nHibernate检索数据库架构 [英] Using nHibernate to retrieve Database Schema

查看:78
本文介绍了使用nHibernate检索数据库架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从nHibernate生成数据库的架构,在这里我向nHibernate提供了对数据库的配置,但是我没有编写任何映射.

Is it possible to generate a schema of a database from nHibernate, where I have provided nHibernate with the configuration to the database but I have not written any mappings.

我希望以编程方式获取数据库MetaData/Schema.

I wish to get the Database MetaData/Schema programmatically.

我正在使用Oracle数据库.我尝试了两种方法:

I am using an Oracle Database. I have tried two approaches:

方法一:

    public DatabaseMetadata GetMetadata(DbConnection connectionIn)
    {
        return new DatabaseMetadata(connectionIn, _dialect);
    }

问题: 不过,这似乎是我所需要的,尽管它可以正确连接,但还没有拿走我的任何桌子.我提供的只是nHibernate Configuration对象,其中填充了我的nHibernate.xml.config文件的内容(连接字符串,驱动程序客户端等).

Problem: This seems to be what I need however, although it correctly connects, it hasn't picked up any of my tables. All I provided was the nHibernate Configuration object which was populated with the contents of my nHibernate.xml.config file (connection string, driver client, etc).

问题: 为什么它不返回表数据?它已正确连接,但一无所获!

Question: Why would it not return the table data? It's connected correctly but finds nothing!

方法二:

 public void DatabaseSchema()
 {
     var schema = new SchemaExport(nHibernateConfiguration);
     schema.SetOutputFile("schema.dll");
      schema.Create(true, true);
 }

nHibernateConfiguration是nHibernate Configuration对象的实例(类的属性),其中填充了nHibernate.xml.config类中的内容.

nHibernateConfiguration is an instance (property on class) of the nHibernate Configuration object, populated with contents from the nHibernate.xml.config class.

问题: 这根本行不通.发生崩溃,但以下情况除外:

Problem: This simply doesn't work. Crashes with the following exception:

NHibernate.MappingException:方言 不支持身份密钥 世代

NHibernate.MappingException : Dialect does not support identity key generation

我怀疑这只会基于您创建的映射生成模式?我没有创建任何映射.我的想法是,这将对我连接到的任何数据库都起作用,从而为其生成架构.

I suspect this will only generate a schema based on mappings you have created? I have created no mappings. The idea is this will work against whichever database I have connected to a generate a schema for it.

问题: 我是否相信此方法将仅基于我的映射生成模式?如果没有,我使用正确吗?

Question: Is my belief that this method will only generate a Schema based on my mappings? If not, Am I using it correctly?

希望这已经足够清楚了,如果我需要提供更多信息,请发表评论.

Hopefully this is clear enough, comment if I need to provide more info.

在此先感谢.

要清楚:我有一个数据库,并且想要获取表示数据库的架构的元数据.

To be clear: I have a database and want to get meta data representing the database, a schema.

推荐答案

NHibernate实际上是基于映射文件的.您可以从它们生成类或表.有一些工具可以生成映射文件,但是它们基于类而不是表.

NHibernate is actually based on the mapping files. You could generate classes or tables from them. There are tools to generate the mapping files, but they are based on the classes, not the tables.

回答您的特定问题:

方法一::NHibernate不会从数据库中读取表定义.所有表定义都需要在映射文件中指定.

Approach one: NHibernate does not read table definitions from the database. All the table definitions need to be specified in the mapping files.

方法二: SchemaExport根据映射定义创建一个SQL文件(创建表,索引等).实际上,建议您使用它,除非您需要处理旧数据库.输出文件应名为* .sql,而不是* .dll.

Approach two: SchemaExport creates an SQL file (Create tables, indexes etc) from the mapping definitions. It is actually recommended to use it, unless you need to cope with legacy databases. The output file should be called *.sql, not *.dll.

您收到的错误很可能是因为您尝试在oracle数据库(或另一个不支持身份列的数据库)上创建身份ID.请改用hilo(或者,如果不喜欢,请使用guid.comb或native).我只是想知道为什么会收到此错误,我以为您没有写任何映射文件?

The error you get is most probably because you try to create an identity id on an oracle database (or another which does not support identity columns). Use hilo instead (or, if you don't like it, guid.comb or native). I just wonder why you get this error, I thought that you didn't write any mapping files?

结论:

我不知道从数据库表创建NHibernate映射文件的任何工具.可能有一个,很可能它不是免费的或不成熟的(因为否则它将是众所周知的).因此,我建议考虑改为生成表定义,或者,如果您有旧数据库,则需要手动编写映射文件.

I don't know of any tool which create NHibernate mapping files from database tables. There may be one, most probably it is not free or not mature (because otherwise it would be well known). So I suggest to think about generating the table definitions instead, or, in case you have a legacy database, you need to go through writing the mapping files manually.

这篇关于使用nHibernate检索数据库架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆