2009-05-03 46 views
8

使用NHibernate开始我的第一步,我试图让它从hbm文件中自动创建我的表格。数据库后端是SQL Server 2008开发版。当“script”为false时,NHibernate SchemaExport不会创建表格

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

var cfg = new Configuration(); 
cfg.Configure(); 
cfg.AddAssembly(typeof(Posting).Assembly); 
new SchemaExport(cfg).Execute(false,true,false,false); 

可悲的是,这是行不通的。我已将show_sql设置为true,并且不打印任何语句。看着SQL事件探查器,我看到我的应用程序连接到数据库,但什么也没做。

我可以修复,通过改变一个参数(“脚本”)设置为true:

new SchemaExport(cfg).Execute(true,true,false,true); 

我不明白为什么。 SchemaExport的参数可悲的是没有真正解释(也没有.Create和.Execute之间的差异),我想知道这个参数的作用,以及为什么它不是必需的,例如当使用SQL Compact Edition时(它也适用于脚本是假的)

回答

27

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

为了让你创建你用.SetOutputFile(文件名)

这是当我想创建一个新的数据库,我使用的方法模式的副本。 我得到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*/); 
} 

SchemaExport.Create只是一起Schema.Execute快捷方式的刚落虚假和真实格式。

public void Create(bool script, bool export) 
    { 
     Execute(script, export, false, true); 
    } 
+0

我用这与NHibernate 3.3,这是写入文件,但不写入数据库。 – 2014-11-20 17:11:17