什么是Fluent NHibernate中的schemaExport?

我很想知道更多关于这段代码以及执行时的预期。

///  /// Sets up NHibernate, and adds an ISessionFactory to the given /// container. ///  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()) //.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().ToConstant(sessionFactory); // Configure a resolver method to be used for creating ISession objects container.Bind().ToMethod(CreateSession); container.Bind().To(); } 

现在我知道它的大部分内容,但我更想知道更多关于这一部分的内容;

 .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. at schemaExport.Create(true, true);

AllUsers是我作为模式的一部分的数据库视图之一

根据要求添加答案

为了解决这个问题,我添加了SchemaAction.None(); 到UserMap如下所示。

 public class UserMap : VersionedClassMap { 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(); Map(x => x.Firstname).Not.Nullable(); Map(x => x.Lastname).Not.Nullable(); Map(x => x.Email).Nullable(); Map(x => x.Username).Not.Nullable(); } } 

错误说Schemaexport尝试两次创建AllUsers,很可能是因为有一个AuxiliaryDatabase对象来创建视图,一个实体映射来使用它。 在AllUsers映射中设置SchemaAction.None()

也:

 .ExposeConfiguration(cfg => { var schemaExport = new SchemaExport(cfg); schemaExport.Drop(true, true); schemaExport.Create(true, true); }) 

可以缩短为

 .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) 

因为Create在创建之前总是会丢弃它会复制丢弃的内容。