【发布时间】: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