HasOne vs引用映射流利的NHibernate [英] HasOne vs References Mapping Fluent NHibernate

查看:131
本文介绍了HasOne vs引用映射流利的NHibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用FluentNhibernate Mapping,面临如何引用另一个表的问题。任何帮助表示赞赏:



我有几个名为CD_ varname 的表,所有这些表都包含两列--CODE和DESCR。 b
$ b

我有一个称为收件人主表,它有两列,分别叫做ALIVE和SEX,他们引用表CD_ALIVE和CD_SEX。
如果在Recipient中Alive = 1,那么我们需要从CD_ALIVE表中得到code = 1的代码和descr。



我描述了一个Codef类:

  public class Codef 
{
int Code {get;组; }
字符串描述{get;组; }
}

我的收件人类将这些分配给一个组件。收件人类看起来像这样:

$ p $ public class IRecepient
{
int ID {get;组; }
出生出生{get;设置;}
死亡死亡{get;组; }
}

在哪里我的出生和死亡类是:

  public class Birth 
{
DateTime BDate {get;组; }
Codef Sex {get;组; }
Codef种族{get;组; } // CD_ETHNICITy Table
Codef Race {get;组; } // CD_RACE Table
}

和我的死亡分类:

  public class Death 
{
DateTime DeathDate {get;组; }
Codef Alive {get;组;所以,Recipient中的主列实际上是引用我的收件人.Death.Alive.Code

我有一个codef映射类:

  public CodefMapping()
{
Map(x => x.Code,CODE);
Map(x => x.Descr,DESCR);
}

我试图做收件人映射,这是我被卡住的地方。我可以这样做吗:
$ b $ pre $ HasOne< CodefMapping>(c => c.Death.Alive)
.PropertyRef(c => c.Code)
.PropertyRef(c => c.Descr)
.WithForeignKey(ALIVE);

无法正常工作:(

谢谢。

解决方案

我想你要使用 References mapping



HasOne 映射在一起共享一个互斥标识符

http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/


This is the first time I am working with FluentNhibernate Mapping and facing a question of how to reference another table. Any help is appreciated:

I have several tables named CD_varname and all these contain two columns - CODE and DESCR.

I have one main table called Recipient and it has, say two columns, called ALIVE and SEX, both are of type number, and they reference to the tables CD_ALIVE and CD_SEX. If Alive=1 in the Recipient, then we need to get the code and descr from CD_ALIVE table where Code=1.

I have described a Codef class:

public Class Codef
{
    int Code { get; set; }
    string Descr { get; set; }
}

My Recipient Class assigns these to a component. Recipient class looks like this:

 public Class IRecepient
{
    int ID { get; set; }
    Birth Birth {get; set;}
    Death Death { get; set; }
}

Where my Birth and Death classes are:

public Class Birth
{
    DateTime BDate { get; set; }
    Codef Sex { get; set; }
    Codef Ethnicity { get; set; } //CD_ETHNICITy Table
    Codef Race { get; set; } //CD_RACE Table
}

and my Death Class:

public Class Death
{
    DateTime DeathDate { get; set; }
    Codef Alive { get; set; }
}   

so, the main column "Alive" in Recipient is actually referencing my Recipient.Death.Alive.Code

I Have a codef mapping class:

   public CodefMapping()
         {
             Map(x => x.Code, "CODE");
             Map(x => x.Descr, "DESCR");
         }

I am trying to do the recipient mapping and this is where I am stuck. Can I do something like this:

 HasOne<CodefMapping>(c => c.Death.Alive)
                    .PropertyRef(c => c.Code)
                    .PropertyRef(c => c.Descr)
                    .WithForeignKey("ALIVE");

It is not working :( Any help is greatly appreciated.

Thank you.

解决方案

I think you want to use the References mapping

HasOne means that the 2 entities that you are mapping together share a "mutually exclusive" identifier

http://jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

这篇关于HasOne vs引用映射流利的NHibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆