Question

I am starting with an existing blank SQL Server 2005 database, configured by FluentNHibernate in the following manner:

ISessionFactory nhibernateSessionFactory = Fluently
    .Configure()
    .Mappings(m => assemblies.ForEach(asm => m.FluentMappings.AddFromAssembly(asm)))
    .Database(
        MsSqlConfiguration.MsSql2005
            .ShowSql()
            .ConnectionString(DatabaseConfig.Instance.ConnectionString))
    .ExposeConfiguration(c => new SchemaUpdate(c).Execute(true, true))
    .BuildSessionFactory();

I have this map:

public abstract class AccountSystemMapBase<T> : ClassMap<T>
{
     protected AccountSystemMapBase()
     {
         Schema("`AccountSystem`");
     }
}

public class UserMap : AccountSystemMapBase<User>
{
    public UserMap() : base()
    {
        Table("`User`");
        ...
    }
}

I want the User table to be created inside a database schema "AccountSystem", instead of the default dbo schema, so I assumed that adding Schema(...) to my mapping would do this.

What actually happened is that when I attempt to perform a query on the User table, I received an exception, saying "object [AccountSystem].[User] does not exits". Indeed, to my surprise, there was no sign of a db schema named AccountSystem into my database, meaning the schema and tables were not created at all. I am using verion 3.1 of NHibernate, which is currently the latest to be supported by Fluent NHibernate. Any ideas/workarounds to make the creation of the schema work?

Was it helpful?

Solution

Nibernate does not create databases nor user schemas; only the table structures within them.

You need to create your database first (unless you use embedded databases that are created automatically by the driver, but that's not the case with SQL Server)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top