views:

1261

answers:

3

Hi,

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.

+3  A: 

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/

Jon Erickson
A: 

References is for a property mapping.

public DeathMap()
{
    References( x => x.Alive );        
}

And you need an Id for codef.

public CodefMapping()
{
    Id(x => x.Code);
    Map(x => x.Descr);
}

The default convention is for column names to match the property name, so you do not have to specify the column names unless they are different.

Lachlan Roche
In that case of separate mapping for Death,how should I specify in Recipient Mapping ?Should it be:public RecipientMapping() { Id(x => x.ID); Map(x => x.Death); } But how will it know that it should bind the "Alive" column in recipient to "code" column in CD_Alive table ?Sorry, I might be dumb in nhibernate at this stage.Thanks.
Aparna
A: 

In the DeathMap, if I have like this:

public DeathMap() 
{ 
    References( x => x.Alive );         
} 

how can I specify the Table_name and the Column name ?

In recipient table, the column name is "ALIVE" and this is referncing the column name "CODE" in the table name "CD_ALIVE".

How can I specify all this information in the Mapping file ?

Does references have an option of specifying table name and the column name is I believe the one that is being referenced to "CODE", but how will I specify the main one that is linking it ?

I really would need to understand this before I go further, because I have ot of tables in the exisitng database and my domain model is huge.

Thank you.

Aparna