尝试使用Castle Active Record检索行时的NHibernate.ObjectNotFoundException

时间:2013-07-26 12:27:57

标签: activerecord castle

我在MsSQL2012中有一个简单的数据库,有两个表我就是这样创建的:

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 for Department ---&gt; NHibernate.ObjectNotFoundException:没有行   使用给定的标识符e xists [ActiveRecordDemo.Domain.Company#4]
  在   NHibernate.Impl.SessionFactoryImpl.DefaultEntityNotFoundDelegate.HandleEntityNotFound(字符串   entityName,Object id)at   NHibernate.Event.Default.DefaultLoadEventListener.Load(比如LoadEvent   event,IEntityPersister persister,EntityKey keyToLoad,LoadType   选项)at   NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(比如LoadEvent   event,IEntityPersister persister,EntityKey keyToLoad,LoadType   选项)at   NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(比如LoadEvent   event,LoadType loadType)at   NHibernate.Impl.SessionImpl.FireLoad(LoadEvent事件,LoadType   loadType)at NHibernate.Impl.SessionImpl.InternalLoad(String   entityName,Object id,Boolean eager,Boolean isNullable)at   NHibernate.Type.EntityType.ResolveIdentifier(Object id,   ISessionImplementor会议)   NHibernate.Type.EntityType.ResolveIdentifier(对象值,   ISessionImplementor session,Object owner)at   NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity,Boolean   readOnly,ISessionImplementor session,PreLoadEvent preLoadEvent,   PostLoadEvent postLoadEvent)at   NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList的   hydratedObjects,Object resultSetId,ISessionImplementor session,   布尔值readOnly)at   NHibernate.Loader.Loader.DoQuery(ISessionImplementor session,   QueryParameters queryParameters,Boolean returnProxies)at   NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor   session,QueryParameters queryParameters,Boolean returnProxies)at   NHibernate.Loader.Loader.DoList(ISessionImplementor session,   QueryParameters queryParameters)at   NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor   session,QueryParameters queryParameters)at   NHibernate.Loader.Loader.List(ISessionImplementor session,   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()

当我得到公司一切都没问题,但是检索部门我得到了上述异常。我犯了什么错误?

1 个答案:

答案 0 :(得分:0)

我可以通过将 NotFoundBehaviour .Ignore参数添加到 BelongsToAttribute 来消除此异常:

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

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

不确定它是解决方案还是解决方法)但是一切正常并且所有单元测试都通过了。