从NHibernate的生成数据库模式脚本 [英] Generating Database Schema Script from NHibernate

查看:270
本文介绍了从NHibernate的生成数据库模式脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了使用NHibernate的一个.NET 2008 MVC应用程序。在previous开发商没有就得到尽可能生成一个数据库为这个项目。
我是相当新的NHibernate的,而我试图找出什么是为创建使用当前映射一个新的数据库创建数据库脚本的最佳解决方案。
我已经通过这个网站很多帖子了,但还是不完全了解如何得到这个工作。
任何指导是AP preciated。

I have inherited a .NET 2008 MVC application that uses nhibernate. The previous developer didn't get as far as generating a database for this project. I am fairly new to nhibernate, and I am trying to figure out what would be the optimal solution for creating a database script for creating a new database using the current mappings. I have gone through many posts on this site, but still do not understand completely how to get this to work. Any guidance is appreciated.

感谢您!

推荐答案

假设你的hbm.xml映射和有效的NHibernate的配置文件,你可以写在一个控制台应用程序生成SQL架构下code:

Assuming you have hbm.xml mappings and a valid nhibernate config file, you could write the following code in a console app to generate the SQL schema:

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

如果你有一口流利的映射,它应该看起来更像是这样的:

If you have fluent mappings, it should look more like this:

Fluently.Configure().Database(MsSqlConfiguration.MsSql2005).Mappings(
            m => m.FluentMappings.AddFromAssemblyOf<Product>()).ExposeConfiguration(
                config =>
                    {
                        new SchemaExport(config).SetOutputFile("schema.sql").Execute(true, false, false);
                    }).BuildConfiguration();

这篇关于从NHibernate的生成数据库模式脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆