使用NHibernate和FluentNHibernate创建数据库和表?

问题描述 投票:1回答:2

我正在寻找用NHibernate和FluentNHibernate创建databasetables的方法。

我怎么能这样做?

试。

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;
}
c# nhibernate fluent-nhibernate
2个回答
5
投票

NHibernate和FNH都不会自己创建数据库。您必须自己提供代码或预先创建的数据库。但它可以为您创建表格。这样做的是调用SchemaExport。在Fluent中,它看起来像这样:

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

无耻地从this SO question复制。


3
投票

我将举一个例子:你创建了3个类:Genre.cs,GenreMap.cs和NHibernateHelper.cs在Genre.cs中你会发现:

using System;
using System.Collections.Generic;
    using  System.Linq;
        using System.Web;



namespace TelerikMvcApp1.Models
{
    public class Genre
    {

            public virtual int GenreId { get; set; }
            public virtual string Name { get; set; }
            public virtual string Description { get; set; }



    }
}

在GenreMap.cs中,您会发现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FluentNHibernate.Mapping;

namespace TelerikMvcApp1.Models
{
    public class GenreMap : ClassMap <Genre>
    {
            public GenreMap()
            {
                Id(x => x.GenreId).Column("Id");
                Map(x => x.Name);
                Map(x => x.Description);
                Table("Genres");
            }

    }
}

在NHibernate Helper中你会发现:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using FluentNHibernate;
    using NHibernate;
    using Npgsql;
    using FluentNHibernate.Cfg;
    using FluentNHibernate.Cfg.Db;
    using NHibernate.Tool.hbm2ddl;

    namespace TelerikMvcApp1.Models
    {
        public class NHibernateHelper
        {
            public static ISession OpenSession()
            {
                ISessionFactory sessionFactory = Fluently
                    .Configure()
                    .Database(PostgreSQLConfiguration.PostgreSQL81
                    .ConnectionString(c => c.Is("Server=localhost;Port=5432;Database=/*your db name*/;User Id=/*your id*/;Password= /*your passwoed*/;"))
                    .ShowSql())

                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<GenreMap>())



                    .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
                    .BuildSessionFactory();
                return sessionFactory.OpenSession();

            }
        }
    }

并且不要忘记指定正确的数据库ID和密码最后,您可以转到您的控制器并将您的索引方法设置为:

public ActionResult Index()
            {

                using (ISession session = NHibernateHelper.OpenSession())
                {
                    var persons = session.Query<Genre>().ToList();

                    return View(persons);
                }



        }
© www.soinside.com 2019 - 2024. All rights reserved.