c# - fluent nhibernate one to many collection, filtered by enum -


i think have design issue here.

essentially have class called office

class office {     public virtual long id { get; set; }     public virtual string code { get; set; }     public virtual ilist<person> managers { get; set; }     public virtual ilist<person> developers { get; set; }     public virtual ilist<person> teamakers { get; set; } } 

and class called person

class person {     public virtual long id { get; set; }     public virtual string name {get; set;}     public virtual stafftype type { get; set;}     public virtual office office { get; set; } } 

and enum called stafftype

public enum stafftype {    manager,    developer,    teamaker } 

mapping person table easy:

public class personmap: classmap<person>     {     public personmap()    {      table("person");      id(x => x.id);      map(x => x.name);      references(x => x.office).foreignkey("id").not.nullable()      map(x => x.type).customtype<stafftype>();    } } 

but stumped on office map. how map use enum filter 3 lists?

if this:

public class officemap: classmap<office>     {     public static string tablename = "office";     public static string mappingcolumn = tablename + "id";      public officemap()    {       table(tablename);       id(x => x.id);       map(x = x.code);        hasmany(x => x.managers)             .cascade.alldeleteorphan()             .fetch.select()             .inverse().keycolumn(mappingcolumn);       hasmany(x => x.developers)             .cascade.alldeleteorphan()             .fetch.select()             .inverse().keycolumn(mappingcolumn);      hasmany(x => x.teamakers)             .cascade.alldeleteorphan()             .fetch.select()             .inverse().keycolumn(mappingcolumn);    } } 

fluency won't have foggiest idea how split 3 collections stafftype enum

thanks help

extra note: person table's type field allways gets mapped int.

nhibernate supports filtering part of mapping. please, read here more 6.2. mapping collection.

the trick add more sql mapping. in fact, condition, evaluated during collection load. small extract documentation:

<map // or <set or <bag ...     name="propertyname"                                         (1)     table="table_name"                                          (2)     ...     where="arbitrary sql condition"                       (9) 

and description of where:

where (optional) specify arbitrary sql condition used when retrieving or removing collection (useful if collection should contain subset of available data)

in case, fluent syntax similar: ...where("mycolumn = 'myvalue' ");

a draft solution:

... hasmany(x => x.managers)    .cascade.alldeleteorphan()    .fetch.select()    .inverse().keycolumn(mappingcolumn)    .where("type = 1") // column name in person table ;                     // , value 1 enum of manager ... // same others 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -