0

所以,我有一个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; }
}

最后,发票付款

public class InvoicePayment
{
    public virtual long InvoicePaymentId { get; set; }

    public virtual Invoice Invoice{ get; set; }
}

问题是,在我的底层架构中InvoicePayment,我有InvoiceNumberSiteId(到Site对象)和CustomerId(到Customer对象)。

在 InvoiceLineItem 中,我将 InvoiceId 链接回Invoice.

因此,我的 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]) 相同

如何调整我的映射,以便加入复合 ID 上的发票付款和身份列上的行项目?

4

1 回答 1

1

错误消息看起来有点奇怪。我希望一个CompositeId()...inInvoiceMap产生这个错误。

该错误源于每个实体只能有 1 个 Id 并且对 CompositeId 的调用会覆盖先前对 Id 的调用,因此 Invoice 的 Id 是 CompositeId 并且 InvoiceLineItem 中的单个列不匹配。

尝试以下操作:

public class Invoice
{
    public virtual long InvoiceId { get; set; }

    public virtual InvoiceIdentity Identity { get; set; }

    public virtual IList<InvoiceLineItem> LineItems { get; set; }
    public virtual IList<InvoicePayment> Transactions { get; set; }

}

public class InvoiceIdentity
{
    public virtual string InvoiceNumber{ get; private set; }
    public virtual Customer Customer { get; private set; }
    public virtual Site Site { get; private set; }
    public Identity(string invoicenumber, Customer customer, Site site)
    {
        InvoiceNumber = invoicenumber;
        Customer = customer;
        Site = site;
    }
}

public sealed class InvoiceMap : ClassMap<Invoice>
{
    public InvoiceMap()
    {
        Table("InvoiceView");

        Id(x => x.InvoiceId).GeneratedBy.Identity();


        Component(x => x.Identity, c =>
        {
            c.Map(x => x.InvoiceNumber);

            c.References(x => x.Site, "SiteId");
            c.References(x => x.Customer, "CustomerId");
        });

        HasMany(x => x.LineItems)
            .Inverse();

        HasMany(x => x.Transactions)
            .PropertyRef(i => i.Identity)
            .KeyColumns.Add("SiteId", "EPayCustomerId", "InvoiceNumber")
            .Inverse();
    }
}

public class InvoicePaymentMap : ClassMap<InvoicePayment>
{
    public InvoicePaymentMap()
    {
        Table("InvoicePayments");

        Id(x => x.InvoicePaymentId).GeneratedBy.Identity();

        References(x => x.Invoice)
            .Columns("SiteId", "CustomerId", "InvoiceNumber")
            .PropertyRef(i => i.Identity)
            .Nullable();

    }
}
于 2012-01-01T18:49:51.080 回答