Search code examples
c#nhibernatelocaldb

NHibernate SchemaExport(config).Create(false, true) does not create DB Schema with SQL Server 2014 LocalDB


I read about 20 question about that problem but find no solution for my specific case so I am here...

I use last version of nhibernate and fluent nhibernate installed via NuGet

All class are public, I have no error on build.

This is my sessionFactory:

public class SessionFactory : IDisposable
{
     private static ISessionFactory _sessionFactory;
     private static Configuration cfg = new NHibernate.Cfg.Configuration();

        private static void InitializeSessionFactory(bool ricreaDatabase = false)
        {
            cfg.Configure(); // read config default style

            var mappings = AutoMap.AssemblyOf<ClasseTest>(new AutoMapConfiguration());
            mappings.WriteMappingsTo(@"c:\mappings");

            Fluently.Configure(cfg)
                .Mappings(m => m.AutoMappings.Add(mappings))
                .ExposeConfiguration(config => new SchemaExport(config).Create(false, true));       
            _sessionFactory = cfg.BuildSessionFactory();
        }

        internal ISession OpenSession()
        {
            NHibernate.ISession session = _sessionFactory.OpenSession();
            return session;
        }

        public SessionFactory()
        {
            InitializeSessionFactory();
        }
}

and my UnitOfWork:

public class UnitOfWork : IUnitOfWork
{
    private ITransaction _transaction;
    private SessionFactory _nhHelper;
    private ISession _session;

    public UnitOfWork()
    {
        _nhHelper = new SessionFactory();
        if (_session == null)
        { _session = _nhHelper.OpenSession(); }
    }

    public void Begin()
    {
        InizializzaSessione();
        _transaction = _session.BeginTransaction();
        Contract.Ensures(_session.Transaction.IsActive);
    }

    public void Save()
    {
        if (_transaction != null)
        {
            _transaction.Commit();
            _transaction = null;
        }
    }
}

In test project I have the hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Server=(LocalDB)\v11.0; Integrated Security=true;AttachDbFilename=C:\Mauro\Sviluppo\Gedi\Test\Gedi.IntegrationTest.DataAccess\DbTest.mdf;</property>
    <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
    <property name="show_sql">true</property> 

  </session-factory>
</hibernate-configuration>

and the test method

public void canCreateClasseTest()
    {

        using (IUnitOfWork uow = new UnitOfWork())
        {
            uow.Begin();
            uow.ServiceRepositoryFor<ClasseTest>().Create(fixture.Create<ClasseTest>());
            uow.Save();
        }
    }

ClasseTest is a simply class I create for this example and I force AutoMapConfiguration to map only it

But after the call of Begin I see no table in the database, so when repository call _session.SaveOrUpdate(obj) i get the "no persister for" error

I look at map generated and this is the code:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="Gedi.Domain.Object.Entity.ClasseTest, Gedi.Domain.Object, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`ClasseTest`">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <property name="Desc" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Desc" />
    </property>
  </class>
</hibernate-mapping>

I tried

  • <property name="hbm2ddl.auto">create</property> in config file
  • cfg.SetProperty("hbm2ddl.auto", "create"); in SessionFactory
  • SchemaExport(cfg).Execute(true, true, false, session.Connection, Console.Out); after the OpenSession in the SessionFactory

but nothing changes

What am I missing?


Solution

  • the problem is there:

    cfg.Configure(); // read config default style
    
                var mappings = AutoMap.AssemblyOf<ClasseTest>(new AutoMapConfiguration());
                mappings.WriteMappingsTo(@"c:\mappings");
    
                Fluently.Configure(cfg)
                    .Mappings(m => m.AutoMappings.Add(mappings))
                    .ExposeConfiguration(config => new SchemaExport(config).Create(false, true));       
                _sessionFactory = cfg.BuildSessionFactory();
    

    mixed fluent and xml configuration simply does not work

    i change it to:

    _sessionFactory = Fluently.Configure()
                    .Mappings(m => m.AutoMappings.Add(mappings))    
                    .Database(
                        MsSqlConfiguration.MsSql2012.ConnectionString("YourConnectionsStringGoesHere")
                            )//End Database
                    .ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
                    .BuildSessionFactory();
    

    and works a bit more

    i will open some new question about more error coming out now when i have time