I got it to work. I needed to have a PropertyRef in my UserAccount
mapping and an Owner property and References in the CreditCard and its
mapping. In addition, when I set the credit card on the UserAccount,
i have to set the owner property of the CreditCard thats getting
passed in. this is similar when you have a bi-directional
relationship between a one-to-many and a many-to-one. You usually
need an AddChild() type method that sets the parent when the child
gets added to the collection: Not sure why NH can't just figure this
stuff out based on the mappings instead of me having to do it in the
object model, but it works.
Heres what worked:
public class UserAccount : DomainObject
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string CompanyName { get; set; }
public virtual string Email { get; set; }
public virtual string Phone { get; set; }
public virtual string AltPhone { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual IDictionary<AddressType, Address> Addresses
{ get; protected set; }
private CreditCard creditCard;
public virtual CreditCard CreditCard
{
get { return this.creditCard; }
set
{
this.creditCard = value;
this.creditCard.Owner = this; //have to set the owner in my
object model for some reason
}
}
. . .
}
public class CreditCard : DomainObject
{
public virtual string NameOnCard { get; set; }
public virtual string CardNumber { get; set; }
public virtual string SecurityCode { get; set; }
public virtual DateTime ExpirationDate { get; set; }
public virtual CardType CardType { get; set; }
public virtual UserAccount Owner { get; set; } //the owner in the
credit card, which gets stored as "OwnerID" in the table
}
public class UserAccountMap : MapBase<UserAccount>
{
public override void CreateMappings()
{
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.CompanyName);
Map(x => x.AltPhone);
Map(x => x.Phone);
Map(x => x.Email);
Map(x => x.Username);
Map(x => x.Password);
HasOne(x => x.CreditCard)
.PropertyRef(c=>c.Owner) //again the owner- i guess this sets up
the FK OwnerID on the CreditCard table?
.FetchType.Join()
.Cascade.All();
HasMany(x => x.Addresses).AsMap(x=>x.Type).Cascade.All();
}
}
public class CreditCardMap : MapBase<CreditCard>
{
public override void CreateMappings()
{
LazyLoad();
Map(x => x.CardNumber);
Map(x => x.ExpirationDate);
Map(x => x.NameOnCard);
Map(x => x.SecurityCode);
References(x => x.Owner).Unique().Cascade.SaveUpdate
().LazyLoad(); //not sure if need this references...
References(x => x.CardType).Cascade.SaveUpdate();
}
}