2013-07-26 43 views
0

我在MsSQL2012一個簡單的數據庫,我已經喜歡這個創建了兩個表:NHibernate.ObjectNotFoundException

CREATE TABLE Company 
(
Id int NOT NULL IDENTITY, 
Name varchar(255), 
PRIMARY KEY (Id) 
)  

CREATE TABLE Department 
(
Id int NOT NULL PRIMARY KEY IDENTITY, 
Name varchar(255), 
Company_Id int NOT NULL, 
FOREIGN KEY (Company_Id) REFERENCES Company(Id) 
); 

通訊城堡的活動記錄類:

公司

[ActiveRecord] 
    public class Company : ActiveRecordBase<Company> 
    { 
     [PrimaryKey] 
     public int Id { get; set; } 

     [Property] 
     public string Name { get; set; } 

     [HasMany(typeof(Department))] 
     public IList<Department> Departments { get; set; } 
    } 

[ActiveRecord] 
    public class Department : ActiveRecordBase<Department> 
    { 
     [PrimaryKey] 
     public int Id { get; set; } 

     [Property] 
     public string Name { get; set; } 

     [HasMany(typeof(Employee))] 
     public IList<Employee> Employees { get; set; } 

     [BelongsTo(Type = typeof(Company), Column = "Id")] 
     public Company Company 
     { 
      get; set; 
     } 
    } 

C#代碼來獲得公司和部門:

ActiveRecordStarter.Initialize(ActiveRecordSectionHandler.Instance, typeof(Company), typeof(Department)); 
var companys = Company.FindAll();//ALL IS FINE, I get a list of companies 
var departments = Department.FindAll();//HERE IS EXCEPTION 

異常

未處理的異常: Castle.ActiveRecord.Framework.ActiveRecordException:無法執行 的FindAll爲部門---> NHibernate.ObjectNotFoundException:沒有行 與給定的標識符重新xists [ActiveRecordDemo.Domain.Company#4]
在 NHibernate.Impl.SessionFactoryImpl.DefaultEntityNotFoundDelegate.HandleEntityNotFound(字符串 的entityName,對象ID)在 NHibernate.Event.Default.DefaultLoadEventListener.Load(比如LoadEvent 事件,IEntityPersister持留,的EntityKey keyToLoad,LoadType 選項)在 NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(比如LoadEvent 事件,IEntityPersister持留,的EntityKey keyToLoad,LoadType 選項)在 NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(比如LoadEvent 事件, LoadType loadType) NHibernate.Impl.SessionImpl.FireL OAD(比如LoadEvent事件,LoadType loadType)在NHibernate.Impl.SessionImpl.InternalLoad(字符串 的entityName,對象ID,布爾渴望,布爾ISNULLABLE)在 NHibernate.Type.EntityType.ResolveIdentifier(對象ID, ISessionImplementor會議)在 NHibernate.Type.EntityType.ResolveIdentifier(對象值, ISessionImplementor會話,對象所有者)在 NHibernate.Engine.TwoPhaseLoad.InitializeEntity(對象實體,布爾 只讀的,ISessionImplementor會話,preLoadEvent preLoadEvent, PostLoadEvent postLoadEvent)在 NHibernate.Loader .Loader.InitializeEntitiesAndCollections(IList hydratedObjects,Object resultSetId,ISessionImplementor session, 布爾只讀)在 NHibernate.Loader.Loader.DoQuery(ISessionImplementor會話, QueryParameters queryParameters,布爾returnProxies)在 NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor 會話,QueryParameters queryParameters,布爾returnProxies)在 NHibernate.Loader。 Loader.DoList(ISessionImplementor會話, QueryParameters queryParameters)在 NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor 會話,QueryParameters queryParameters)在 NHibernate.Loader.Loader.List(ISessionImplementor會話, QueryParameters queryParameters,ISet的1 querySpaces, IType[] resultTypes) at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session) at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) at NHibernate.Impl.CriteriaImpl.List(IList results) at NHibernate.Impl.CriteriaImpl.List() at Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType, DetachedCriteria detachedCriteria, Order[] orders) --- End of inner exception stack trace --- at Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType, DetachedCriteria detachedCriteria, Order[] orders) at Castle.ActiveRecord.ActiveRecordBase.FindAll(Type targetType) at Castle.ActiveRecord.ActiveRecordBase 1。FindAll()

當我得到公司一切正常,但與檢索部門我得到了上述例外。我犯了什麼錯誤?

回答

0

我可以通過添加擺脫這種異常NotFoundBehaviour結尾。可以忽略參數BelongsToAttribute

[BelongsTo(Type = typeof(Company), Column = "Id", NotFoundBehaviour = NotFoundBehaviour.Ignore)] 

[BelongsTo(Type = typeof(Department), Column = "Id", NotFoundBehaviour = NotFoundBehaviour.Ignore)] 

不知道這是一個解決方案或解決方法),但一切工作正常,所有單元測試都通過了。