【问题标题】:Fluent nHibernate keeps recreating my database, why?Fluent nHibernate 不断重新创建我的数据库,为什么?
【发布时间】:2009-11-08 19:02:25
【问题描述】:

我正在尝试将我的数据层从 Linq2Sql 转换为 nHibernate。我认为 NHibernate 中的 Xml 配置相当落后,所以我使用的是 Fluent。

我已经设法变得流利,添加了存储库模式和工作单元模式,并且我的单元测试看起来不错。

但是现在当我将它插入我的服务层时,我注意到每次运行我的应用程序时,数据库都会重新创建。

我猜这取决于我的 SessionProvider 代码,我不确定我正在使用的所有扩展。有人可以阐明如何阻止这种情况发生吗?

public sealed class SessionProvider
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory CreateSessionFactory()
    {
        try
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2005
                              .ConnectionString(Properties.Settings.Default.DBConnection)
                              .Cache(c => c
                                              .UseQueryCache()
                                              .ProviderClass<HashtableCacheProvider>())
                              //.ProxyFactoryFactory("NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHiber nate.ByteCode.Castle")
                              .ShowSql())
                              .Mappings(m=>m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message);
            return null;
        }


    }

    public static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                _sessionFactory = CreateSessionFactory();
            }
            return _sessionFactory;
        }
    }

    public static ISession GetSession()
    {
        return SessionFactory.OpenSession();
    }

    private static void BuildSchema(Configuration config)
    {
        // this NHibernate tool takes a configuration (with mapping info in)
        // and exports a database schema from it
        new SchemaExport(config).Create(false, true);
    }
}

【问题讨论】:

    标签: asp.net asp.net-mvc nhibernate fluent-nhibernate


    【解决方案1】:

    删除这一行

    .ExposeConfiguration(BuildSchema)
    

    阅读更多关于 new SchemaExport(config).Create(false, true); here

    其实最后一个参数就是要创建数据库了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2010-12-13
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      相关资源
      最近更新 更多