map table to itself in Parent-Child relationship Fluent Nhibernate -
i have situation mapping table's columns primary key of same table. table looks this
---+-------+--------- id name parentid ---+-------+--------- 1 parent1 0 2 child 1 1 3 child 2 1 4 parent2 0 5 child 3 4
i have created following model , fluent nhibernate mapping class
//model.locationtype.cs public class locationtype { public virtual long id { get; set; } public virtual string shortname { get; set; } public virtual string description { get; set; } public virtual ilist<locationtype> parentid { get; set; } }
and
//mapping.locationtypemap.cs public class locationtypemap : classmap<locationtype> { public locationtypemap() { table("set_loc_type"); id(x => x.id).column("loc_type_id").generatedby.assigned(); map(x => x.shortname, "short_name").length(15).not.nullable(); map(x => x.description, "loc_desc").length(50).not.nullable(); references(x => x.parentid).column("parent_loc_type_id").cascade.saveupdate(); } }
but receiving follow error message when execute code:
unable cast object of type 'smarthrms.core.domain.model.locationtype' type 'system.collections.generic.ilist`1[smarthrms.core.domain.model.locationtype]'.
edit 1: instead of using tried
hasmany(x => x.parentids).keycolumn("parent_loc_type_id");
although worked , solved casting problem mentioned above result gettting reverse of need. in parent's locationtype objects, lists childs in ilist, above example result be:
-----+----------+------ id name parentid -----+----------+------ 1 parent1 ilist<child1, child2> 2 child 2 ilist<empty> 3 .... same 4 parent2 ilist<child3> 5 child 3 ilist<empty>
seems should use hasmany
in mapping instead of references
.
Comments
Post a Comment