0

我在 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 ---> NHibernate.ObjectNotFoundException:没有具有给定标识符的行存在 [ActiveRecordDemo.Domain.Company#4]
在 NHibernate.Impl.SessionFactoryImpl.DefaultEntityNotFoundDelegate.HandleEntityNotFound(String entityName, Object id) 在 NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent 事件,IEntityPersister 持久化,EntityKey keyToLoad,LoadType 选项) 在 NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad (LoadEvent 事件,IEntityPersister 持久化,EntityKey keyToLoad,LoadType 选项)在 NHibernate.Impl.SessionImpl.FireLoad(LoadEvent 事件,LoadType loadType)在 NHibernate.Impl 的 NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent 事件,LoadType loadType)。 NHibernate.Type.EntityType.ResolveIdentifier(Object id, ISessionImplementor session) 在 NHibernate.Type 的 SessionImpl.InternalLoad(String entityName, Object id, Boolean eager, Boolean isNullable)。NHibernate.Engine.TwoPhaseLoad.InitializeEntity 的 EntityType.ResolveIdentifier(对象值,ISessionImplementor 会话,对象所有者)(对象实体,布尔只读,ISessionImplementor 会话,PreLoadEvent preLoadEvent,PostLoadEvent postLoadEvent)在 NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydradObjects,对象在 NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) 在 NHibernate.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) 在 NHibernate.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) NHibernate.Loader.Loader 中的 .DoList(ISessionImplementor session, QueryParameters queryParameters)。ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) 在 NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet1 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.ActiveRecordBase1.查找全部()

当我得到公司时,一切都很好,但是在检索部门时,我得到了上述异常。我在哪里做错了?

4

1 回答 1

0

我可以通过将NotFoundBehaviour .Ignore 参数添加到 BelongsToAttribute 来摆脱这个异常:

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

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

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

于 2013-07-30T15:47:17.967 回答