NHibernate SchemaExport 和 Configure() catch-22 [英] NHibernate SchemaExport and Configure() catch-22

查看:15
本文介绍了NHibernate SchemaExport 和 Configure() catch-22的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在新项目中使用 DDD 并首先为我的课程建模,然后根据类库生成数据库模式.我的计划是使用 NHibernate hbm2ddl 工具 SchemaExport 来做到这一点.

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.

问题是我无法让 SchemaExport 工作,因为它让我陷入了一个奇怪的 catch-22 问题.SchemaExport 需要一个 配置对象,它本身需要一个有效的NHibernate配置文件以及一组数据库映射.

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.

这里的 catch-22 是当我执行 Configure() 时,它会抱怨 无法确定实体 'MyEntity' 的表名称;删除 'table' 属性或为其分配一个值." 所以 Configure() 方法要求表存在,而 SchemaExport 应该根据 Configuration 创建它> 我无法创建,因为该表不存在.

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.

那么,我到底应该如何创建一个有效的 NHibernate Configuration 包含 SchemaExport 所需的映射,以便在没有 Configure() 的情况下实际做一些有用的事情 抛出并抱怨它找不到要使用 SchemaExport 创建的表?是否有一个模式"我可以设置 Configuration 对象,这样它就不会检查数据库是否存在给定的表,或者我还需要做些什么?

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?

推荐答案

你能发布你的配置文件吗?

Can you post your configuration file?

我一直使用这种方法,不存在表,并且能够即时生成模式.我的猜测是您的 .hbm 文件之一中可能有一些内容.尝试将您的架构减少到 1 个表,使其工作,然后从那里构建它.作为参考,这是我用来生成数据库架构的代码:

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:

    var cfg = new Configuration();
    cfg.Configure();
    var schema = new SchemaExport(cfg);
    schema.Create(true, true);

这也会为您将脚本推送到控制台,因此您可以查看针对数据库生成的 SQL.

This will also push the script to the console for you, so you can see what SQL is generated against the db.

这篇关于NHibernate SchemaExport 和 Configure() catch-22的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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