当"script"为false时,NHibernate SchemaExport不会创建表

Mic*_*tum 8 .net c# nhibernate

使用NHibernate完成我的第一步,我正在尝试从hbm文件自动创建表.数据库后端是SQL Server 2008 Developer Edition.

这是我在NHibernate教程中看到的常见示例代码:

var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Posting).Assembly);
new SchemaExport(cfg).Execute(false,true,false,false);
Run Code Online (Sandbox Code Playgroud)

可悲的是,这不起作用.我已将show_sql设置为true,并且它不会打印出任何语句.查看SQL分析器,我看到我的应用程序连接到数据库,但后来什么也没做.

我可以通过将第一个参数("script")更改为true来解决这个问题:

new SchemaExport(cfg).Execute(true,true,false,true);
Run Code Online (Sandbox Code Playgroud)

我不明白为什么.遗憾的是没有真正解释SchemaExport的参数(也没有解释.Create和.Execute之间的区别),我想知道这个参数的作用,以及为什么不需要它,即使用SQL Compact Edition时(也适用于脚本是假的)

Mag*_*gie 28

SchemaExport是Hbm2Ddl实用程序的一部分,它实际上与NHibernate功能分开.它不使用在NHibernate仅运行时使用的"show_sql".

要获取您创建的模式的副本,请使用.SetOutputFile(filename)

这是我希望创建新数据库时使用的方法.我在MyDDL.sql文件中获得格式化的模式,数据库是从模式构建的:

 private void BuildSchema(Configuration config)
 {

        new SchemaExport(config)
            .SetOutputFile(_fileName + "MyDDL.sql")
            .Execute(true /*script*/, true /*export to db*/,
                     false /*just drop*/, true /*format schema*/);
 }
Run Code Online (Sandbox Code Playgroud)

SchemaExport.Create只是Schema.Execute的快捷方式,只需删除false和格式为true.

public void Create(bool script, bool export)
    {
        Execute(script, export, false, true);
    }
Run Code Online (Sandbox Code Playgroud)