NHibernate SchemaExport(配置).Create(false, true)不使用SQL Server

本文关键字:SQL Server true false SchemaExport 配置 Create NHibernate | 更新日期: 2023-09-27 18:16:10

关于这个问题,我读了大约20个问题,但没有找到适合我具体情况的解决方案,所以我在这里…

我使用最新版本的nhibernate和通过NuGet安装的fluent nhibernate

所有的类都是公共的,我在构建时没有错误。

这是我的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();
        }
}

和我的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;
        }
    }
}

在测试项目中我有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>

和试验方法

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

ClasseTest是我为这个例子创建的一个简单的类,我强制AutoMapConfiguration只映射它

但是在调用Begin之后,我在数据库中没有看到表,所以当存储库调用_session.SaveOrUpdate(obj)时,我得到"no persister for"错误

查看生成的map,代码如下:

<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>

我试着

  • <property name="hbm2ddl.auto">create</property>在配置文件
  • SessionFactory中的cfg.SetProperty("hbm2ddl.auto", "create"); SessionFactory 中OpenSession后的
  • SchemaExport(cfg).Execute(true, true, false, session.Connection, Console.Out);

但没有变化

我错过了什么?

NHibernate SchemaExport(配置).Create(false, true)不使用SQL Server

问题就在那里:

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

混合流畅和XML配置根本不起作用

我把它改成:

_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();

和工作更多一点

当我有时间的时候,我会打开一些关于更多错误的新问题