NHibernate配置错误 - 连接字符串?

时间:2012-04-16 20:36:53

标签: asp.net sql nhibernate

我收到此错误:

------测试开始:汇编:ECEHire.Test.dll ------

测试'ECEHire.Test.Tests.GenerateSchema_Fixture.Can_generate_schema'失败:NHibernate.HibernateException:初始化字符串的格式不符合从索引49开始的规范。   ----> System.ArgumentException:初始化字符串的格式不符合从索引49开始的规范。     at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action 1 scriptAction, Boolean export, Boolean justDrop) at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop) GenerateSchema_Fixture.vb(18,0): at ECEHire.Test.Tests.GenerateSchema_Fixture.Can_generate_schema() --ArgumentException at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(String connectionString, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) at System.Data.SqlClient.SqlConnection.ConnectionString_Set(String value) at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) at NHibernate.Connection.DriverConnectionProvider.GetConnection() at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action 1 scriptAction,Boolean export,Boolean justDrop)

0传递,1失败,0跳过,耗时3.23秒(NUnit 2.5.10)。

当我尝试测试我的NHibernate配置文件时。

配置文件是:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.connection_string">Data Source=MyServerName;initial catalog=MyDatabaseName;Integrated Security=SSPI</property>
    <property name="connection.isolation">ReadCommitted</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
  </session-factory>
</hibernate-configuration>

我正在运行的测试是:

<Test()> _
Public Sub Can_generate_schema()

    Dim cfg = New Configuration()
    cfg.Configure()
    cfg.AddAssembly(GetType(Question).Assembly)
    Dim exp As NHibernate.Tool.hbm2ddl.SchemaExport = New NHibernate.Tool.hbm2ddl.SchemaExport(cfg)
    exp.Execute(False, True, False)

End Sub

我认为这是一个连接字符串错误,但连接字符串是web.config文件的一部分,用于通过ADO查询数据库。

1 个答案:

答案 0 :(得分:0)

要尝试的一些事项: -

  1. 它可以像添加一样简单; (半结肠)例如Integrated Security=SSPI;
  2. 或尝试更改为Integrated Security=True;
  3. 或放弃connection.connection_string并使用connection.connection_string_name
  4. 在web.config中

    <connectionStrings>
      <add name="db" connectionString="Data Source=MyServerName;initial catalog=MyDatabaseName;Integrated Security=SSPI;"/>
    </connectionStrings>
    
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
      <session-factory>
        ...
        <property name="connection.connection_string_name">db</property>
        ...
      </session-factory>
    </hibernate-configuration>
    

    编辑接下来要尝试的是确保您的测试中使用了配置文件,请尝试以下操作: -

    cfg.Configure(@"c:\hibernate.cfg.xml");
    

    确保路径完全正确,如果失败,则在此行设置一个断点: -

    cfg.AddAssembly(GetType(Question).Assembly)
    

    然后添加此监视cfg.Properties并确保连接字符串作为键值对存在

    <强> EDIT2 对于单元测试,您需要从执行程序集中复制本地true和get路径或创建app.config并使用: -

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="hibernate-configuration"
          type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
      </configSections>
      <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
        <session-factory>
          <property ../>
        </session-factory>
      </hibernate-configuration>
    </configuration>
    

    对于您可以执行的网站: -

    cfg.Configure(HttpContext.Current.Server.MapPath("~/hibernate.cfg.xml"));
    

    说实话,有很多选择,这是另一个问题!