流利 - 在身份上与一个孩子的关系,在复合键上与其他孩子的关系
因此,我有一个像这样的 Invoice
对象:
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; }
}
然后,我有一个像这样的发票行项目
public class InvoiceLineItem
{
public virtual long InvoiceLineItemId { get; set; }
public virtual Invoice Invoice{ get; set; }
}
最后,一个发票 Payment
public class InvoicePayment
{
public virtual long InvoicePaymentId { get; set; }
public virtual Invoice Invoice{ get; set; }
}
问题是这样的,在我的 InvoicePayment
的底层架构中>,我有 InvoiceNumber
、SiteId
(到 Site
对象)和 CustomerId
(到 客户对象)。
在 InvoiceLineItem 中,我将 InvoiceId 链接回 Invoice
。
因此,我的发票映射看起来像这样:
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();
}
}
行项目映射
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");
}
}
最后是我的发票付款映射
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();
}
}
因此,事实上,我收到了错误
外键 (FKE9F746C567E71B3F:InvoiceLineItems [InvoiceId])) 的列数必须与引用的主键 (InvoiceView [SiteId, CustomerId, InvoiceNumber]) 相同
相同身份栏上的行项目?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息看起来有点奇怪。我希望
InvoiceMap
中的CompositeId()...
会产生此错误。该错误源于以下事实:每个实体只能有 1 个 Id,并且对 CompositeId 的调用会覆盖之前对 Id 的调用,因此 Invoice 的 Id 是 CompositeId,并且 InvoiceLineItem 中的单个列不匹配。
尝试以下操作:
the error message looks a bit weird. I would expect a
CompositeId()...
inInvoiceMap
to 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: