使用(Fluent-)NHibernate保持简单树会导致System.InvalidCastException

时间:2010-12-29 20:05:38

标签: c# nhibernate fluent-nhibernate recursion mono

似乎存在递归数据结构和(Fluent-)NHibernate或者它只是我的问题,是一个完整的白痴......

这是树:

公共类SimpleNode {

public SimpleNode ()
{
    this.Children = new List<SimpleNode> ();
}

public virtual SimpleNode Parent { get; private set; }
public virtual List<SimpleNode> Children { get; private set; }

public virtual void setParent (SimpleNode parent)
{
    parent.AddChild (this);
    Parent = parent;
}

public virtual void AddChild (SimpleNode child)
{
    this.Children.Add (child);
}

public virtual void AddChildren (IEnumerable<SimpleNode> children)
{
    foreach (var child in children) {
        AddChild (child);
    }
}

}

映射:

public class SimpleNodeEntity:ClassMap&lt; SimpleNode&gt;

{

public SimpleNodeEntity ()
{
    Id (x => x.Id);

    References (x => x.Parent).Nullable ();

    HasMany (x => x.Children).Not.LazyLoad ().Inverse ().Cascade.All ().KeyNullable ();
}

}

现在,每当我尝试保存节点时,我都会这样:

System.InvalidCastException:无法从源类型转换为目标类型。 at(包装器动态方法)SimpleNode。 (对象,对象[],NHibernate.Bytecode.Lightweight.SetterCallback)NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(object,object [])at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(object,object [])< / p>

我的设置:

Mono 2.8.1(在OSX上),NHibernate 2.1.2,FluentNHibernate 1.1.0

1 个答案:

答案 0 :(得分:1)

为了记录:我有点通过'ChildReferences'关系替换'Children'关系来捏造问题,将实际的'Children'保留为瞬态(这些是通过'ChildReferences'间接构建的):

...

public virtual IList&lt;ChildReference&gt; ChildReferences { get; private set; }

...

在映射中:

HasMany (x => x.ChildReferences).Cascade.All ();

public class ChildReference 
    {
        public virtual int ChildId { get; set; }
    }  

...