orm - Fluent NHibernate Mapping (one to one with conditional) -


i'm trying "clean" poorly designed database structure (at least in orm).

the table structure this:

table: members memberid (int pk) username (varchar)  table: addresses addressid (int pk) memberid (int, not set fk - awesome) firstname (varchar) lastname (varchar) addressline1 (varchar) isbillingaddress (bit) 

so created 2 classes (entities), 1 customer , 1 address.

public class customer {     public virtual int customerid { get; set; }     public virtual string firstname     {         { return billingaddress.firstname; }         set { billingaddress.firstname = value; }     }     public virtual string lastname     {         { return billingaddress.lastname; }         set { billingaddress.lastname = value; }     }     public virtual address billingaddress { get; set; }     public virtual address shippingaddress { get; set; } }  public class address {     public virtual customer customer { get; set; }     public virtual int addressid { get; set; }     public virtual string firstname { get; set; }     public virtual string lastname { get; set; }     public virtual string addressline1 { get; set; }     public virtual string addressline2 { get; set; }     public virtual string city { get; set; }     public virtual string state { get; set; }     public virtual string postalcode { get; set; } } 

the query retrieve customers billing address be:

select top 1 * dbo.address isbilling = 1 , memberid = @memberid 

there should 1 billing , shipping address per customer. far, fluent classmap looks this:

public class customermapping : classmap<customer> {     public customermapping()     {         table("members");          id(m => m.customerid).column("memberid");         map(m => m.billingaddress);          hasone(x => x.billingaddress).propertyref("memberid");         hasone(x => x.shippingaddress).propertyref("memberid");     } } 

i'm not sure if should using hasone... should 1 one mapping. how can "where" clause in there (i.e. where address.memberid = members.customerid) distinguish between billing , shipping? also, top 1? know can maybe use join dont see fluent function add clause.

modifying database structure unfortunately not option.

thanks

you don't want hasone(), though sounds right. hasone() means tables share primary keys. use references() instead.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -