c# - NHibernate Mapping By Code Many-to-One with an Intermediate Table -
here scenario. have 2 classes, husband , wife :d , relation between these 2 classes defined through third intermediate table named people.
classes:
class husband { public virtual int husbandid { get; set; } public virtual wife wife { get; set; } } class wife { public virtual int wifeid { get; set; } ... }
tables:
husband :: table husbandid : int wife :: table wifeid : int people :: table peopleid : int manid : int womanid : int relationtype : int
in people table, relationtype = 1 indicates marriage relation between man , woman manid == husbandid , womanid == wifeid.
note guaranteed there 1 wife each husband in people table. needless cannot modify tables. legacy database.
mapping:
class husbandmap : classmapping<husband> { public husbandmap() { id(x => x.husbandid); manytoone(x => x.wife); // <-- how make mapping work ? } } class wifemap : classmapping<wife> { public wifemap() { id(x => x.wifeid); } }
now question how can have many-to-one mapping husband wife using intermediate table people?
so far found ungly-hacky solution works bad :d
class husbandmap : classmapping<husband> { public husbandmap() { id(x => x.husbandid); join("people", j => { j.key(x => x.column("manid , relationtype = 1"); //here sql injection hack :d j.manytoone(x => x.wife); }); } }
the problem facing cannot find other way push relationtype = 1
generated sql. knows how done?
Comments
Post a Comment