流利 - 在身份上与一个孩子的关系,在复合键上与其他孩子的关系

发布于 2024-12-23 00:50:33 字数 3047 浏览 1 评论 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; }
}

最后,一个发票 Payment

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

    public virtual Invoice Invoice{ get; set; }
}

问题是这样的,在我的 InvoicePayment 的底层架构中>,我有 InvoiceNumberSiteId (到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-12-30 00:50:33

错误消息看起来有点奇怪。我希望 InvoiceMap 中的 CompositeId()... 会产生此错误。

该错误源于以下事实:每个实体只能有 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();

    }
}

the error message looks a bit weird. I would expect a CompositeId()... in InvoiceMap 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:

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();

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文