首页 文章

NHibernate - SchemaExport,SchemaUpdate没有创建数据库/模式

提问于
浏览
0

我是NHibernate和Fluent NHibernate的新手 . 我想要实现的是选择适当的数据库提供程序并单击按钮,生成数据库(如果不存在)并创建所有表,关系等 .

在上面描述的开始我遇到了一个问题 . 即,以下代码不创建数据库 - 作为返回我收到错误 . 同样的情况是NHibernate与xml和Fluent NHibernate . 如果我手动创建数据库,代码工作正常,则会创建表 . 但如果它不存在,不是 .

这是NHibernate和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="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.Sql2012ClientDriver</property>
    <property name="connection.connection_string">Server=.\SQLEXPRESS;Database=databaseName;Initial Catalog=databaseName;User id=user;password=xxx</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
  </session-factory>
</hibernate-configuration>

这里是流畅的NHibernate方法:

_sessionFactory = Fluently.Configure()
                        .Database(MsSqlConfiguration.MsSql2012
                        .ConnectionString(@"Server=.\SQLEXPRESS;Database=databaseName;Initial Catalog=databaseName;User id=user;password=xxx")
                        .ShowSql())
                        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Entry>())
                        .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) 
                        .BuildSessionFactory();

所有评论的方法(SchemaUpdate和添加属性)都没有给出任何积极的结果 . 我仍然收到以下错误:

Cannot open database "databaseName" requested by the login. The login failed.Login failed for user 'user'.

我在Visual Studio 2015中尝试过MSSQL Express 2012和localdb . 我知道最后我应该使用SchemaUpdate来放弃数据,但是对于开发我可以使用任何尝试过的方法 .

我希望有可能在此解决方案中支持以下数据库:MSSQL,Oracle,MySQL,PostgreSQL .

请告诉我我做错了什么 . 谢谢 .

1 回答

  • 1

    SchemaExport不会创建数据库 . 它只创建表和其他数据库对象 .

    创建数据库通常非常耗费配置,需要更多权限 . 这可能是它从未作为SchemaExport的一部分实现的原因 .

相关问题