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

标签 .net asp.net-mvc nhibernate nhibernate-mapping

我继承了一个使用 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/

相关文章:

.net - 为列表添加事件处理程序

.net - WebMethod 响应格式

javascript - 使用 Web API 在 CRM 中添加新记录

c# - NHibernate 延迟加载 - session 关闭后

c# - 有没有办法知道 C# Windows 窗体应用程序启动期间发生了什么

.net - 单个 .NET 进程是否有内存限制

c# - 如何通过 Jquery Ajax Get 将对象传递给 MVC Controller

c# - 如何在 MVC4 中将 Json 字符串输出为 JsonResult?

nhibernate - 在Fluent NHibernate映射中指定紧急加载

NHibernate:什么是子 session ,为什么以及何时应该使用它们?