gpt4 book ai didi

.net - 从 NHibernate 生成数据库模式脚本

转载 作者:行者123 更新时间:2023-12-04 18:09:45 26 4
gpt4 key购买 nike

我继承了一个使用 nhibernate 的 .NET 2008 MVC 应用程序。以前的开发人员并没有为此项目生成数据库。
我对 nhibernate 相当陌生,我试图找出使用当前映射创建新数据库的数据库脚本的最佳解决方案。
我在这个网站上浏览了很多帖子,但仍然不完全理解如何让它工作。
任何指导表示赞赏。

谢谢!

最佳答案

假设您有 hbm.xml 映射和有效的 nhibernate 配置文件,您可以在控制台应用程序中编写以下代码来生成 SQL 模式:

//load initial config from hibernate config file
Configuration cfg = new Configuration().Configure("hibernate.cfg.xml");

//add assembly in which the hbm.xml mappings are embedded (assuming Product class is in this assembly)
cfg.AddAssembly(typeof(Product).Assembly);

//this will generate the SQL schema file in the executable folder
new SchemaExport(cfg).SetOutputFile("schema.sql").Execute(true, false, false);

如果你有流畅的映射,它应该看起来更像这样:
Fluently.Configure().Database(MsSqlConfiguration.MsSql2005).Mappings(
m => m.FluentMappings.AddFromAssemblyOf<Product>()).ExposeConfiguration(
config =>
{
new SchemaExport(config).SetOutputFile("schema.sql").Execute(true, false, false);
}).BuildConfiguration();

关于.net - 从 NHibernate 生成数据库模式脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17220159/

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