Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NHibernate通过代码多对一和中间表进行映射_C#_Nhibernate Mapping_Many To One_Mapping By Code - Fatal编程技术网

C# NHibernate通过代码多对一和中间表进行映射

C# NHibernate通过代码多对一和中间表进行映射,c#,nhibernate-mapping,many-to-one,mapping-by-code,C#,Nhibernate Mapping,Many To One,Mapping By Code,下面是一个场景。我有两个类,比如丈夫和妻子:D,这两个类之间的关系是通过第三个名为People的中间表定义的 课程: 桌子: 在People表中,RelationType=1表示男性和女性之间的婚姻关系,其中ManId==丈夫ID,WomanId==妻子ID 请注意,可以保证People表中每个丈夫只有一个妻子。不用说,我也不能修改这些表。它是遗留数据库 映射: classhursbandmap:ClassMapping { 公共林地图() { Id(x=>x.HusbandId); 许多人(

下面是一个场景。我有两个类,比如丈夫和妻子:D,这两个类之间的关系是通过第三个名为People的中间表定义的

课程: 桌子: 在People表中,RelationType=1表示男性和女性之间的婚姻关系,其中ManId==丈夫ID,WomanId==妻子ID

请注意,可以保证People表中每个丈夫只有一个妻子。不用说,我也不能修改这些表。它是遗留数据库

映射:
classhursbandmap:ClassMapping
{
公共林地图()
{ 
Id(x=>x.HusbandId);
许多人(x=>x.layer);//x.WifeId);
}
}
现在的问题是,如何使用中间表People实现从丈夫到妻子的多对一映射

到目前为止,我发现了一个笨拙的黑客解决方案,它可以工作,但它只是不好:D

class HusbandMap : ClassMapping<Husband>
{
  public HusbandMap()
  { 
    Id(x => x.HusbandId);
    Join("People", j =>
       {
          j.Key(x => x.Column("manId and relationType = 1"); //here is the sql injection hack :D
          j.ManyToOne(x => x.Wife);
       });
  }
}
classhursbandmap:ClassMapping
{
公共林地图()
{ 
Id(x=>x.HusbandId);
加入(“人”,j=>
{
j、 Key(x=>x.Column(“manId和relationType=1”);//下面是sql注入hack:D
j、 多酮(x=>x.妻子);
});
}
}
我面临的问题是,我找不到任何其他方法将
relationType=1
推送到生成的sql。有人知道如何做到这一点吗

Husband :: Table
  HusbandId : int

Wife :: Table
  WifeId : int

People :: Table
  PeopleId : int
  ManId : int
  WomanId : int
  RelationType : int
class HusbandMap : ClassMapping<Husband>
{
  public HusbandMap()
  { 
    Id(x => x.HusbandId);
    ManyToOne(x => x.Wife); // <-- How to make this mapping work ?
  }
}

class WifeMap : ClassMapping<Wife>
{
  public WifeMap()
  {
    Id(x => x.WifeId);
  }
}
class HusbandMap : ClassMapping<Husband>
{
  public HusbandMap()
  { 
    Id(x => x.HusbandId);
    Join("People", j =>
       {
          j.Key(x => x.Column("manId and relationType = 1"); //here is the sql injection hack :D
          j.ManyToOne(x => x.Wife);
       });
  }
}