开发者

Persisting simple tree with (Fluent-)NHibernate leads to System.InvalidCastException

there seems to be a problem with recursive data structures and (Fluent-)NHibernate or its just me, being a complete moron...

here's the tree:

public class 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);
}

publ开发者_如何学Goic virtual void AddChildren (IEnumerable<SimpleNode> children)
{
    foreach (var child in children) {
        AddChild (child);
    }
}

}

the mapping:

public class SimpleNodeEntity : ClassMap<SimpleNode>

{

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

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

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

}

now, whenever I try to save a node, I get this:

System.InvalidCastException: Cannot cast from source type to destination type. at (wrapper dynamic-method) SimpleNode. (object,object[],NHibernate.Bytecode.Lightweight.SetterCallback) at NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues (object,object[]) at NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer (object,object[])

My setup:

Mono 2.8.1 (on OSX), NHibernate 2.1.2, FluentNHibernate 1.1.0


For the record: I kinda fudged the problem by replacing the 'Children' relation by a 'ChildReferences' relation, leaving the actual 'Children' as transient (these get built via the 'ChildReferences' indirection):

...

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

...

in the mapping:

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

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

...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜