NHibernate SchemaExport 和Configure() catch-22
我想在新项目中使用 DDD 并首先对我的类进行建模,然后生成基于类库的数据库模式。 我的计划是使用 NHibernate hbm2ddl 工具 SchemaExport
来完成此操作。
问题是我无法让 SchemaExport
工作,因为它给我带来了一个奇怪的 catch-22 问题。SchemaExport
需要 Configuration 对象本身需要一个有效的 NHibernate 配置文件以及一组数据库映射。
这里的 catch-22 是,当我执行配置()时,它抱怨“无法确定实体'MyEntity'的表名称;删除'table'属性或为其分配值。”< /code> 所以
Configure()
方法要求表存在,而 SchemaExport
应该根据我的 Configuration
创建它无法创建,因为该表不存在。
那么,我到底应该如何创建一个有效的 NHibernate Configuration
,其中包含 SchemaExport
所需的映射,以便在没有 Configure()
的情况下实际执行一些有用的操作> 抛出并抱怨找不到要使用 SchemaExport
创建的表? 是否有一种“模式”我可以设置 Configuration
对象,这样它就不会检查数据库中是否存在给定的表,或者我还需要做其他事情吗?
I want to use DDD in a new project and model my classes first, then generate the database schema based on the class library. My plan is to do this with the NHibernate hbm2ddl tool SchemaExport
.
The problem is that I can't get the SchemaExport
to work because of a weird catch-22 problem it puts me in. The SchemaExport
requires a Configuration
object that itself requires a valid NHibernate configuration file as well as a set of database mappings.
The catch-22 here is that when I do the Configure(), it complains "Could not determine the name of the table for entity 'MyEntity'; remove the 'table' attribute or assign a value to it."
So the Configure()
method requires the table to exist, while the SchemaExport
is supposed to create it based on the Configuration
that I can't create because the table isn't doesn't exist.
So, how on earth am I supposed to create a valid NHibernate Configuration
containing the mappings required for SchemaExport
to actually do something useful without having Configure()
throw and complain that it can't find the tables that are to be created with SchemaExport
? Is there a "mode" I can set the Configuration
object in so it won't check the database for the existence of the given tables, or is there something else I need to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以贴一下你的配置文件吗?
我一直在没有表的情况下使用这种方法,并且能够动态生成模式。 我的猜测是您的 .hbm 文件之一可能有问题。 尝试将您的架构削减为 1 个表,使其正常工作,然后从那里构建它。 作为参考,这里是我用来生成数据库模式的代码:
这还将为您将脚本推送到控制台,以便您可以查看针对数据库生成的 SQL。
Can you post your configuration file?
I use this method all the time with no tables present, and am able to generate the schema on the fly. My guess is that you may have something off in one of your .hbm files. Try cutting your schema down to 1 table, getting it to work, then building it up from there. As a reference, here is the code I use to generate the db schema:
This will also push the script to the console for you, so you can see what SQL is generated against the db.