本文介绍了什么是在SchemaExport工具NHibernate的流利?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,想知道更多关于如何执行时,该代码和期望是什么

  ///<总结> ; 
///设置了NHibernate的,并增加了一个ISessionFactory给定的
///容器。
///< /总结>
私人无效ConfigureNHibernate(容器的iKernel)
{
//构建NHibernate的ISessionFactory对象
VAR SessionFactory的= FluentNHibernate
.Cfg.Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008.ConnectionString(
C => c.FromConnectionStringWithKey(ServicesDb)))
.CurrentSessionContext(网)
.Mappings(M = GT; m.FluentMappings.AddFromAssemblyOf< SqlCommandFactory>())
//.ExposeConfiguration(cfg =>新建SchemaUpdate工具(CFG).Execute(真,真))
.ExposeConfiguration(CFG => ;
{
变种的SchemaExport =新的SchemaExport(CFG);
schemaExport.Drop(真实,真实);
schemaExport.Create(真实,真实);
})
.BuildSessionFactory();

//的ISessionFactory实例添加到容器
container.Bind< ISessionFactory方式>()ToConstant(SessionFactory的);

//配置解析方法也可以用于创建的ISession对象
container.Bind&所述; ISession的方式>()ToMethod(了createSession);

container.Bind&所述; ICurrentSessionContextAdapter方式>()到< CurrentSessionContextAdapter>();
}

现在我知道大部分是干什么的,但我更感兴趣的是了解更多关于这一部分;

  .ExposeConfiguration(CFG => 
{
变种的SchemaExport =新SchemaExport工具(CFG);
schemaExport.Drop(真实,真实);
schemaExport.Create(真实,真实);
})

Idealy我期待 schemaExport.Drop(真实,真实); 删除数据库架构和 schemaExport.Create(真实,真实); 来创建它。现在是的SchemaExport 有关的,因为我们知道它?我问这个,因为当我提到的配置上运行我的应用程序我得到一个错误:



目前已经在名为AllUsers的对象。数据库 schemaExport.Create(真实,真实);



AllUsers的是数据库视图我有一个为模式的一部分。



追加答案



要修正错误,我添加了 SchemaAction.None(); 来中userMap如下图所示。

 公共类中userMap:VersionedClassMap<使用者> 
{
公众用户映射()
{
表(AllUsers的); //这是造成此错误
SchemaAction.None数据库视图(); //将其加入到固定porblem

标识(X => x.UserId).CustomType&所述;的Guid>();
地图(X => x.Firstname).Not.Nullable();
地图(X => x.Lastname).Not.Nullable();
地图(X => x.Email).Nullable();
地图(X => x.Username).Not.Nullable();
}
}


解决方案

的错误说的SchemaExport尝试创建AllUsers的两倍,最有可能是因为有一个AuxiliaryDatabase对象创建的视图和实体映射使用它。设置 SchemaAction.None()



另外,在AllUsers的映射:



  .ExposeConfiguration(CFG => 
{
变种的SchemaExport =新的SchemaExport(CFG);
schemaExport.Drop(真实的, TRUE);
schemaExport.Create(真实,真实);
})

可以缩短为

  .ExposeConfiguration(CFG =>新建的SchemaExport(CFG).Create(真,真))

由于创建确实一直下降创造,因为它代表它会重复落下之前。


I am curious to know more on how this code and what is expected when executed.

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

Now i know what most of it is doing, but i am more interested to know more about this section;

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

Idealy i expect schemaExport.Drop(true, true); to drop the database schema and schemaExport.Create(true, true); to recreate it. Now, is SchemaExport about the Database Schemas as we know it? I ask this because when i run my application with the mentioned configurations i get an error:

There is already an object named 'AllUsers' in the database. at schemaExport.Create(true, true);

AllUsers is one of the database views i have as part of the schema

Appending Answer as requested

To fix the bug, i added SchemaAction.None(); to UserMap as seen below.

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();
    }
}
解决方案

The error says that Schemaexport tries to create AllUsers two times, most probably because there is an AuxiliaryDatabase object to create the view and an Entity Mapping to use it. Set SchemaAction.None() in the AllUsers mapping.

Also:

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

can be shortened to

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

because Create does always drops before creating it would duplicate the dropping as it stands.

这篇关于什么是在SchemaExport工具NHibernate的流利?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:59