NHibernate SchemaExport and Configure() catch-22

喜夏-厌秋 提交于 2019-12-03 20:25:24

问题


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?


回答1:


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:

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

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



来源:https://stackoverflow.com/questions/1022894/nhibernate-schemaexport-and-configure-catch-22

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!