我很想知道更多关于此代码的方式以及执行时的预期结果。
/// <summary>
/// Sets up NHibernate, and adds an ISessionFactory to the given
/// container.
/// </summary>
private void ConfigureNHibernate(IKernel container)
{
// Build the NHibernate ISessionFactory object
var sessionFactory = FluentNHibernate
.Cfg.Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008.ConnectionString(
c => c.FromConnectionStringWithKey("ServicesDb")))
.CurrentSessionContext("web")
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SqlCommandFactory>())
//.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
.ExposeConfiguration(cfg =>
{
var schemaExport = new SchemaExport(cfg);
schemaExport.Drop(true, true);
schemaExport.Create(true, true);
})
.BuildSessionFactory();
// Add the ISessionFactory instance to the container
container.Bind<ISessionFactory>().ToConstant(sessionFactory);
// Configure a resolver method to be used for creating ISession objects
container.Bind<ISession>().ToMethod(CreateSession);
container.Bind<ICurrentSessionContextAdapter>().To<CurrentSessionContextAdapter>();
}
现在我知道它的大部分在做什么,但我更有兴趣了解更多关于这部分的信息;
.ExposeConfiguration(cfg =>
{
var schemaExport = new SchemaExport(cfg);
schemaExport.Drop(true, true);
schemaExport.Create(true, true);
})
理想情况下我期望schemaExport.Drop(true, true);
删除数据库架构并schemaExport.Create(true, true);
重新创建它。现在是SchemaExport
有关数据库模式 http://www.quackit.com/sql_server/sql_server_2008/tutorial/sql_server_database_schemas.cfm据我们所知?我问这个问题是因为当我使用上述配置运行应用程序时出现错误:
There is already an object named 'AllUsers' in the database.
at schemaExport.Create(true, true);
AllUsers
是我作为架构一部分的数据库视图之一
按要求附加答案
为了修复该错误,我添加了SchemaAction.None();
到 UserMap 如下所示。
public class UserMap : VersionedClassMap<User>
{
public UserMap()
{
Table("AllUsers"); //This is the database view which was causing the error
SchemaAction.None(); // This was added to fix the porblem
Id(x => x.UserId).CustomType<Guid>();
Map(x => x.Firstname).Not.Nullable();
Map(x => x.Lastname).Not.Nullable();
Map(x => x.Email).Nullable();
Map(x => x.Username).Not.Nullable();
}
}