城堡ActiveRecord懒惰加载不工作

时间:2010-12-15 17:52:24

标签: c# nhibernate activerecord repository-pattern castle-activerecord

我需要一些帮助来理解这个问题。我正在使用存储库 使用ActiveRecordMediator进行模式。我启用了会话范围http 模块,使用ActiveRecord标记我的类(Lazy = true)。

问题是我每次执行FindAll或SlicedFindAll时, 中介返回一组初始化元素而不是 代理。有人能指出我正确的方向吗?

这是我的存储库:

public interface IEntityRepository<TEntity>
{
    IList<TEntity> FindAll(int page, int pageSize, out int resultCount);
}

public class EntityRepository<TEntity> : IEntityRepository<TEntity> 
{
    public virtual IList<TEntity> FindAll(int page, int pageSize)
    {
        return (IList<TEntity>)ActiveRecordMediator.SlicedFindAll(typeof(TEntity), (page * pageSize), pageSize);
    }
}

[ActiveRecord(Lazy = true)]
public class DocumentEntity
{
    private Guid _id;
    private IList<DocumentVersionEntity> _versions;

    [PrimaryKey(PrimaryKeyType.GuidComb, "Id")]
    public virtual Guid Id
    {
        get { return _id; }
        set { _id = value; }
    }

    [HasAndBelongsToMany(typeof(DocumentVersionEntity), RelationType.Bag, Table = "DocumentEntriesToDocumentVersions", ColumnKey = "DocumentEntryId", ColumnRef = "DocumentVersionId", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)]
    public virtual IList<DocumentVersionEntity> Versions
    {
        get { return _versions; }
        set { _versions = value; }
    }
}

[ActiveRecord(Lazy = true)]
public class DocumentVersionEntity
{
    private Guid _id;

    [PrimaryKey(PrimaryKeyType.GuidComb, "Id")]
    public virtual Guid Id
    {
        get { return _id; }
        set { _id = value; }
        }
    }
}

当我执行FindAll方法时,版本中的所有对象 DocumentEntity的数组是DocumentVersionEntity而不是 DocumentVersionEntityProxy并且都是初始化的。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

在以下代码中,您的级联行为设置为ManyRelationCascadeEnum.AllDeleteOrphan:

 [HasAndBelongsToMany(typeof(DocumentVersionEntity),
                     RelationType.Bag,
                     Table = "DocumentEntriesToDocumentVersions", 
                     ColumnKey = "DocumentEntryId", 
                     ColumnRef = "DocumentVersionId", 
                     Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, 
                     Lazy = true)]
    public virtual IList<DocumentVersionEntity> Versions
    {
        get { return _versions; }
        set { _versions = value; }
    }

这迫使Nhibernate,无论好坏,加载整个集合,以便进行孤儿清理。你必须摆脱级联行为才能发挥作用。将NotFoundBehavior设置为忽略也是如此,[在此处阅读] [1]:http://blog.agilejedi.com/2010/12/nhibernateactiverecord-lazy-loading.html   [1]。