使用NHibernate和FluentNHibernate创建数据库和表? [英] Create Database and Tables with NHibernate and FluentNHibernate?

查看:142
本文介绍了使用NHibernate和FluentNHibernate创建数据库和表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用NHibernate和FluentNHibernate创建databasetables的任何方法.

I'm looking for any way to create database and tables with NHibernate and FluentNHibernate.

我该怎么做?

尝试.

private static ISessionFactory createConnection()
{
    if (session != null)
        return session;

    //database configs
    FluentConfiguration _config = Fluently.Configure().Database(
        MySQLConfiguration.Standard.ConnectionString(
            x => x.Server(HOST).
            Username(USER).
            Password(PASSWORD).
            Database(DB)
        ))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PerfilMap>())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ModuloMap>())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<PermissaoMap>())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<UsuarioMap>())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<CategoriaMap>())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<SubcategoriaMap>    ())
        .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true));

    session = _config.BuildSessionFactory();
    return session;
}

推荐答案

NHibernate和FNH都不创建数据库本身.您必须自己提供代码或预先创建的数据库.但是它可以为您创建表.执行此操作的类称为SchemaExport.在Fluent中,它看起来像这样:

Neither NHibernate nor FNH create the database itself. You have to provide either code or a precreated database yourself. But it can create the tables for you. The class doing this is call SchemaExport. In Fluent, it looks like this:

var sessionFactory = Fluently.Configure()
   .Database(/* ... */)
   .Mappings(/* ... */)
   .ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false))
   .BuildSessionFactory();

此SO问题中无耻地复制.

Shamelessly copied from this SO question.

这篇关于使用NHibernate和FluentNHibernate创建数据库和表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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