database - NHibernate on a table with two "primary" keys -
i'm learning nhibernate in order layer on rather peculiar legacy database. other applications use same live database, can't make changes affect them.
i've run problem because 1 table, represents hardware devices, has 2 columns used de facto primary keys. 1 real primary key, auto-generated row id. other unique , non-null hardware serial number.
many other tables in database have foreign-key relationship table. however, of them use real primary key - integer row id - foreign key, , use hardware id of device instead.
note in practice hardware id , row id, once paired, remain paired.
will able create mappings deal in nhibernate, or need create views give me more standardized schema, , use instead of triggers make them updatable?
the db in use mssql 2000, in case makes difference.
in situation following:
public class hardwaredevice{ public virtual int id {get; set;} public virtual string serialnumber {get; set;} //other stuff } public class domainthinga { public virtual int id {get; set;} public virtual hardwaredevice device {get; set;} //other stuff } public class domainthingb { public virtual int id {get; set;} public virtual hardwaredevice device {get; set;} //other stuff }
map out hardwaredevice class using autogenerated id primary key. examples use fluentnhibernate class maps.
public class hardwaredevicemap : classmap<hardwaredevice> { public hardwaredevicemap(){ id(x=>x.id).generatedby.native().column("id"); //uses auto number map(x=>x.serialnumber).column("serialnumber"); //other mappings } }
now mapping out other 2 classes:
public class domainthingamap : classmap<domainthinga> { public domainthingamap(){ id(x=>x.id).generatedby.native(); //uses auto number references(x=>x.device) .column("deviceid"); //joins on id in hardwaredevice table default //other mappings } } public class domainthingbmap : classmap<domainthingb> { public domainthingbmap(){ id(x=>x.id).generatedby.native(); //uses auto number references(x=>x.device) .column("serialnumber") //column in domainthingb table .propertyref("serialnumber"); //joins using serialnumber column (hardware device table) //other mappings } }
the property-ref feature of class maps allows join on columns not primary key these types of legacy database purposes.
Comments
Post a Comment