5

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();
    }
}
Komengem
  • 3,662
  • 7
  • 33
  • 57
  • Take a look at this one : http://stackoverflow.com/questions/730725/nhibernate-schemaexport-failing-to-drop-a-table-sometimes – granadaCoder Jul 19 '13 at 01:57
  • @granadaCoder The answer in that post is what i have showed here, doesn't work for me. However, `SchemaUpdate` works just fine. But i hear, its not good practice to use `SchemaUpdate`. – Komengem Jul 19 '13 at 04:22
  • I guess what I was getting at was...could you try the more simple approach (to verify it works).......instead of the chained commands........just for a test. – granadaCoder Jul 19 '13 at 12:37
  • var cfg = new Configuration(); cfg.Configure(); cfg.AddAssembly(typeof(Person).Assembly); SchemaExport se = new SchemaExport(cfg); //drop database se.Drop(true, true); //re-create database se.Create(true, true); – granadaCoder Jul 19 '13 at 12:38
  • Because I've had .Drop and .Create ... work well for me in the past.......in order to "create the db structure from scratch" in the past. – granadaCoder Jul 19 '13 at 12:39
  • @granadaCoder Thanks for your suggestion, but it looks like `SchemaAction.None()` is all i needed in my `UserMap` – Komengem Jul 19 '13 at 16:10
  • Can you append (at the bottom) (of your original post) to have the full answer......I'm gonna needt this someday. Thanks. – granadaCoder Jul 19 '13 at 18:28
  • @granadaCoder Not sure, what you would like me to append in the original post. The marked answer plus comments seems self explanatory as to where changes needed to be made. However, if you do come back to this and somehow require more help, you can just ask in the comment and i will be happy to assit. – Komengem Jul 19 '13 at 18:41
  • The append would be the final version of the code that worked for you. Aka, where exactly the "SchemaAction.None()" line was placed. – granadaCoder Jul 19 '13 at 19:05

1 Answers1

5

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.

Firo
  • 30,626
  • 4
  • 55
  • 94
  • AllUsers is a Database View, which is an SQL script, i can't pu `SchemaAction.None()` in there. But i also have UserMap, perhaps you meant that? – Komengem Jul 19 '13 at 11:04
  • All works now, thanks. I am using the longer option because its more readable, unless you think the shorter version is more efficient? – Komengem Jul 19 '13 at 15:35
  • 2
    the shorter version is more efficient but not much because it doesn't drop the schema 2 times but once – Firo Jul 19 '13 at 18:10