What is schemaExport in Fluent NHibernate?

c#, fluent-nhibernate, nhibernate, sql-server

Solution

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.

Problem

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

Original source

Related problems