gpt4 book ai didi

c# - 当 "script"为 false 时,NHibernate SchemaExport 不创建表

转载 作者:太空狗 更新时间:2023-10-29 18:03:15 25 4
gpt4 key购买 nike

使用 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);

遗憾的是,这不起作用。我已将 show_sql 设置为 true,它不会打印出任何语句。查看 SQL 事件探查器,我看到我的应用程序连接到数据库,但随后什么也没做。

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

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

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

最佳答案

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*/);
}

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

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

关于c# - 当 "script"为 false 时,NHibernate SchemaExport 不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/817359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com