![](/img/trans.png)
[英]Nhibernate exception: base {NHibernate.HibernateException} = {“illegal access to loading collection”}
[英]NHibernate rises NHibernate.HibernateException : Could not find named connection
我想用 Active Record + MSSQL2012 嘗試一個簡單的例子。 這是我的 App.config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<startup>
</startup>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect, NHibernate</property>
<property name="connection.connection_string_name">Server=(local);initial catalog=DemoDB;Integrated Security=SSPI</property>
</session-factory>
</hibernate-configuration>
</configuration>
在本地,我有一個非常簡單的數據庫DemoDB,其中包含 3 個表,如果通過 SQL Management Studio 連接到。
員工類:
使用 Castle.ActiveRecord;
namespace ActiveRecordDemo.Domain { [ActiveRecord] public class Employee : ActiveRecordBase { [PrimaryKey] public int Id { get; 放; }
[Property] public string FirstName { get; set; } [Property] public string LastName { get; set; } [BelongsTo(Type = typeof(Department), Column = "Id")] public Department Department { get; set; } } }
當我運行代碼
ActiveRecordStarter.Initialize(ActiveRecordSectionHandler.Instance, typeof(Company), typeof(Department), typeof(Employee)); IList 員工 = Employee.FindAllByProperty("FirstName", firstName);
我收到一個錯誤:
NHibernate.HibernateException : 找不到命名連接字符串 Server=(local);initial catalog=DemoDB;Integrated Security=SSPI
連接有什么問題?
屬性connection.connection_string_name
強制 NHibernate 在 App.Config 的標准<connectionStrings>
部分枚舉的字符串中搜索連接字符串。 您有兩個選擇:使用屬性connection.connection_string
而不是connection.connection_string_name
,將連接字符串放置到特殊部分:
<connectionStrings>
<add name="MyConnString" connectionString="Server=(local);initial catalog=DemoDB;Integrated Security=SSPI" />
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect, NHibernate</property>
<property name="connection.connection_string_name">MyConnString</property>
</session-factory>
對我來說,根網站的物理路徑沒有路徑。 在我的情況下,根站點僅用於保存單個應用程序。 我們所有的網站都是由代碼創建的,IIS 不進行驗證,它只接受您告訴它的內容......包括空白字段。
在 IIS 中打開應用程序設置會出現“無法讀取配置”錯誤。
修復是管理網站 --> 高級設置填寫物理路徑(空白)確定
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.