c# - How to attach an object which is not persisted to an object loaded from DB using NHIbernate -


i have entity looks likes this:

public class driver {     ivehicle vehicle {get;set;} } 

and several different classes implement ivehicle (car, bike, train...)

how can select specific implementation of ivehicle driver holds, , remember selection next time load driver db?

i'm using nhibernate 3.0 persistence fluentnhibernate mapping.

edit

ok figured out finally. needed map vehicle , map each subclass in following way:

public void vehiclemap : classmap<ivehicle> {     public vehiclemap()     {         id(v => v.id); // needed add property used id          discriminatesubclassesoncolumn("type");     }  }  public void caremap : subclassmap<car> {     public carmap()     {          discriminatorvalue("car");     } } 

additionally, needed disable lazy loading of vehicle driver mapping

you need decide how inheritance mapping work. see documentation http://nhibernate.info/doc/nh/en/index.html#inheritance

once do, can map driver class cascade vehicle dependency. after point nhibernate automatically load proper item database.

update

you need persist type name string. vehicle class can unmapped property like:

public class driver {     private string vehicletypename;     private ivehicle vehicle;      public ivehicle vehicle     {                 {             if (vehicle == null)             {                 vehicle = typeof(ivehicle).assembly                                    .createinstance(vehicletypename);             }             return vehicle;         }     } } 

Comments