I have a simple database in MsSQL2012 with two tables which I've created like this:
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)
);
Corresponding Castle active record classes:
Company
[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; }
}
Department
[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# code to get companies and departments:
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
Exception:
Unhandled Exception: Castle.ActiveRecord.Framework.ActiveRecordException: Could not perform FindAll for Department ---> NHibernate.ObjectNotFoundException: No row with the given identifier e xists[ActiveRecordDemo.Domain.Company#4]
at NHibernate.Impl.SessionFactoryImpl.DefaultEntityNotFoundDelegate.HandleEntityNotFound(String entityName, Object id) at NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) at NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options) at NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType) at NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType) at NHibernate.Impl.SessionImpl.InternalLoad(String entityName, Object id, Boolean eager, Boolean isNullable) at NHibernate.Type.EntityType.ResolveIdentifier(Object id, ISessionImplementor session) at NHibernate.Type.EntityType.ResolveIdentifier(Object value, 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, Boolean 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, 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.ActiveRecordBase
1.FindAll()
When I get companies all is OK, but with retrieving departments I get the above exception. Where I made a mistake?
I could get rid of this exception by adding NotFoundBehaviour.Ignore parameter to BelongsToAttribute:
[BelongsTo(Type = typeof(Company), Column = "Id", NotFoundBehaviour = NotFoundBehaviour.Ignore)]
and
[BelongsTo(Type = typeof(Department), Column = "Id", NotFoundBehaviour = NotFoundBehaviour.Ignore)]
Not sure if it is a solution or workaround ) but all is working fine and all unit tests passed.