C# 当“脚本”为假时,NHibernate SchemaExport 不创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/817359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:22:27  来源:igfitidea点击:

NHibernate SchemaExport does not create tables when "script" is false

c#.netnhibernate

提问by Michael Stum

Making my first steps with NHibernate, I'm trying to have it creating my Tables automatically from the hbm files. The database backend is SQL Server 2008 Developer Edition.

使用 NHibernate 迈出第一步,我试图让它从 hbm 文件自动创建我的表。数据库后端是 SQL Server 2008 Developer Edition。

This is the common sample code I see in NHibernate Tutorials:

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

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

Sadly, this does not work. I have set show_sql to true, and it does not print out any statement. Looking at SQL profiler I see my Application connecting to the DB, but then doing nothing.

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

I can fix that by changing the first parameter ("script") to true:

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

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

I don't understand why. The parameters of SchemaExport are sadly not really explained (also not the difference between .Create and .Execute), and I would like to find out what this parameter does, and why it's not needed i.e. when using SQL Compact Edition (that works also when script is false)

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

采纳答案by Maggie

The SchemaExport is part of the Hbm2Ddl utility which is really separate from NHibernate functionality. It does not use "show_sql" which is used while NHibernate is running only.

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

To get a copy of the schema you create you use .SetOutputFile(filename)

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

This is the method I use when I wish to create a new database. I get a formatted schema in MyDDL.sql file and the database is built from the schema:

这是我希望创建新数据库时使用的方法。我在 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 is just a shortcut to Schema.Execute with the just drop false and format true.

SchemaExport.Create 只是 Schema.Execute 的快捷方式,只需删除 false 并设置为 true。

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