views:

307

answers:

1

I need a clue how to map my Invoice class.

public class Buyer
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
    public virtual string TaxRegNo { get; set; } 
    // .... more properties....
}

public class Invoice
{
    public virtual int Id { get; set; }

    public virtual int IdBuyer { get; set; }
    public virtual Buyer Buyer { get; set; } 
    // ....more properties
}

The problem is that I want to have in Invoice class:

  • BuyerId - just an integer ID for reference and foregin key relationship
  • a copy of almost all buyer properties (its accounting document and properties cannot be changed after confirmation) - as component

I tried to this using following mapping but it doesn't work

    public InvoiceMap()
    {
        Id(x => x.Id);

        References(x => x.IdBuyer);
        Component(x => x.Buyer, BuyerMap.WithColumnPrefix("buyer_"));
    // ....more properties
    }
A: 

You would normally not map both the foreign key and the child object. If you do map both, then do this in the mapping (or similar):

References(x => x.Buyer);
Map(x => x.IdBuyer).Column("BuyerId").Not.Insert().Not.Update();

Then you don't double up on the column name in SQL statements, which causes errors around mismatched numbers of parameters.

David M