c# - HasMany gives null children -
i'm having trouble defining tree in fluent nhibernate. i've done other hasmany relationships before not self referencing this.
no matter try, children == null.
entity:
public class stockcontainer {     public virtual guid id { get; set; }      public virtual string name { get; set; }      public virtual stockcontainer parent { get; set; }     public virtual ilist<stockcontainer> children { get; set; }      public virtual void moveto(stockcontainer outercontainer)     {         parent = outercontainer;     } }   fluent nhibernate mapping:
public class stockcontainermapping : classmap<stockcontainer> {     public stockcontainermapping()     {         table("stockcontainers");         id(x => x.id);         map(x => x.name).unique();         references(n => n.parent).lazyload().nullable();         hasmany(n => n.children).keycolumn("parent_id").where(x => x.parent.id == x.id);     } }   generated table:
create table stockcontainers (     id uniqueidentifier not null,    name text unique,    parent_id uniqueidentifier,    primary key (id),    constraint fkb5fa0632a80e0632 foreign key (parent_id) references stockcontainers )   unit test fails:
    [testmethod]     public void can_move_an_item()     {         var item1 = loadbyname("item1"); //test helper function loads items repository         var item2 = loadbyname("item2");          //pair them         using (var transaction = _session.begintransaction())         {             item2.moveto(item1);             transaction.commit();         }          //reload them         item1 = loadbyname("item1");         item2 = loadbyname("item2");          assert.areequal(item1, item2.parent); //ok         assert.isnotnull(item1.children);     //fails here         assert.areequal(1, item1.children.count);     }      
ah, must use session before loading them again or same entities.
    _session = createsession(); //(test class method create session)      //reload them     var item1b = loadbyname("item1");     var item2b = loadbyname("item2");      //check have loaded new objects     assert.arenotsame(item1, item1b);     assert.arenotsame(item2, item2b);      
Comments
Post a Comment