什么是Fluent NHibernate中的schemaExport?

时间:2013-07-19 01:06:42

标签: 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>();
        }

现在我知道它的大部分内容,但我更感兴趣的是了解更多有关此部分的内容;

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

Idealy我希望schemaExport.Drop(true, true);删除数据库架构,并schemaExport.Create(true, true);重新创建它。现在,SchemaExport是关于我们所知道的Database Schemas吗?我问这个,因为当我使用上述配置运行我的应用程序时,我收到一个错误:

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();
    }
}

1 个答案:

答案 0 :(得分:4)

错误说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在创建之前总是会丢失,因此它会复制丢弃的内容。