So, I have an Invoice object like so:
public class Invoice
{
public virtual long InvoiceId { get; set; }
public virtual string InvoiceNumber{ get; set; }
public virtual Customer Customer { get; set; }
public virtual Site Site { get; set; }
public virtual IList<InvoiceLineItem> LineItems { get; set; }
public virtual IList<InvoicePayment> Transactions { get; set; }
}
Then, I have an invoice line item like this
public class InvoiceLineItem
{
public virtual long InvoiceLineItemId { get; set; }
public virtual Invoice Invoice{ get; set; }
}
And finally, an invoice Payment
public class InvoicePayment
{
public virtual long InvoicePaymentId { get; set; }
public virtual Invoice Invoice{ get; set; }
}
The problem is this, in my underlying schema for InvoicePayment, I have InvoiceNumber, SiteId (to the Site object), and CustomerId (to the Customer object).
In InvoiceLineItem, I have InvoiceId linking back to Invoice.
So, my mapping for Invoice looks something like this:
public sealed class InvoiceMap : ClassMap<Invoice>
{
public InvoiceMap()
{
Table("InvoiceView");
Id(x => x.InvoiceId).GeneratedBy.Identity();
Map(x => x.InvoiceNumber);
References<Site>(x => x.Site, "SiteId");
References<Customer>(x => x.Customer, "CustomerId");
HasMany<InvoiceLineItem>(x => x.LineItems)
.Inverse();
HasMany<InvoicePayment>(x => x.Transactions)
.KeyColumns.Add("SiteId")
.KeyColumns.Add("EPayCustomerId")
.KeyColumns.Add("InvoiceNumber")
.Inverse();
}
}
Line Items mapping
public class InvoiceLineItemMap : ClassMap<InvoiceLineItem>
{
public InvoiceLineItemMap()
{
Table("InvoiceLineItems");
Id(x => x.InvoiceLineItemId).GeneratedBy.Identity();
References<FTNI.Core.Model.Invoice.Invoice>(x => x.Invoice, "InvoiceId");
}
}
And finally my Invoice payments mapping
public class InvoicePaymentMap : ClassMap<InvoicePayment>
{
public InvoicePaymentMap()
{
Table("InvoicePayments");
Id(x => x.InvoicePaymentId).GeneratedBy.Identity();
CompositeId()
.KeyProperty(x => x.Site, "SiteId")
.KeyProperty(x => x.Customer, "CustomerId")
.KeyProperty(x => x.InvoiceNumber);
References<Site>(x => x.Site, "SiteId");
References<EPayCustomer>(x => x.Customer, "CustomerId");
References<FTNI.Core.Model.Invoice.Invoice>(x => x.Invoice)
.Columns("SiteId", "CustomerId", "InvoiceNumber")
.Nullable();
}
}
So, as it is, I am getting an error
Foreign key (FKE9F746C567E71B3F:InvoiceLineItems [InvoiceId])) must have same number of columns as the referenced primary key (InvoiceView [SiteId, CustomerId, InvoiceNumber])
How can I adjust my mappings so I join to the invoice payments on a composite id and the line items on the identity column?
the error message looks a bit weird. I would expect a
CompositeId()...inInvoiceMapto produce this error.The error stems from the fact that each entity can only have 1 Id and the call to CompositeId overrides the previous call to Id, hence the Id of Invoice is the CompositeId and a single Column in InvoiceLineItem doesnt match.
try the following: