1

我收到此错误:

------ 测试开始:程序集:ECEHire.Test.dll ------

测试'ECEHire.Test.Tests.GenerateSchema_Fixture.Can_generate_schema'失败:NHibernate.HibernateException:初始化字符串的格式不符合从索引49开始的规范。----> System.ArgumentException:初始化字符串的格式不符合从索引 49 开始的规范。在 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(Action1 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 查询数据库。

4

1 回答 1

0

A few things to try:-

  1. Could it be something as simple as adding a ; (semi colon) e.g. Integrated Security=SSPI;
  2. Or try changing to Integrated Security=True;
  3. Or drop connection.connection_string and use connection.connection_string_name

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

Edit The next thing to try is to make sure that the configuration file is being used in your test so try this:-

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

Making sure that the path IS totally correct, If this fails then put a break point at this line:-

cfg.AddAssembly(GetType(Question).Assembly)

and then add this watch cfg.Properties and make sure that the connection string is present as a key value pair

Edit2 For unit testing you would need to copy local true and get path from executing assembly OR create an app.config and use:-

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

For a web site you could do:-

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

To be honest there are lots of options and this is really another question!

于 2012-04-17T07:50:00.957 回答