Fluent NHibernate 中的 schemaExport 是什么?

Kom*_*gem 5 c# sql-server nhibernate fluent-nhibernate

我很想了解更多有关此代码的方式以及执行时的预期结果的信息。

        /// <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>();
        }
Run Code Online (Sandbox Code Playgroud)

现在我知道它的大部分在做什么,但我更有兴趣了解更多关于这一部分的信息;

.ExposeConfiguration(cfg =>
                          {
                              var schemaExport = new SchemaExport(cfg);
                              schemaExport.Drop(true, true);
                              schemaExport.Create(true, true);
                           })
Run Code Online (Sandbox Code Playgroud)

理想情况下,我希望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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Fir*_*iro 5

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

还:

.ExposeConfiguration(cfg =>
                      {
                          var schemaExport = new SchemaExport(cfg);
                          schemaExport.Drop(true, true);
                          schemaExport.Create(true, true);
                       })
Run Code Online (Sandbox Code Playgroud)

可以缩短为

.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
Run Code Online (Sandbox Code Playgroud)

因为 Create 总是在创建之前丢弃,所以它会按原样复制丢弃。

  • 较短的版本更有效,但效率并不高,因为它不会删除模式 2 次,而是一次 (2认同)