我很想知道更多关于这段代码如何以及执行时的预期。
/// <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
关于我们所知道的数据库模式吗?我问这个是因为当我使用上述配置运行我的应用程序时出现错误:
There is already an object named 'AllUsers' in the database.
在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();
}
}