映射同一类中的两个引用,但引用类具有复合键

发布于 2024-12-01 05:02:59 字数 1493 浏览 1 评论 0原文

我有以下数据库表(为了清楚起见,这些表已缩写):

CREATE TABLE [dbo].[prod_uom](
    [prod_id] [dbo].[uid] NOT NULL,       --Primary key
    [uom_type] [numeric](9, 0) NOT NULL,  --Primary Key
)

CREATE TABLE [dbo].[order_line](
    [order_line_id] [dbo].[uid] NOT NULL,  --Primary key
    [prod_id] [dbo].[uid] NOT NULL,        --Foreign key
    [uom_type_requested] [numeric](9, 0) NOT NULL, --Foreign key
    [uom_specified] [numeric](9, 0) NOT NULL        --Foreign key
)

我遇到问题的实体如下所示(缩写):

public class OrderLine
{
     public virtual Guid OrderLineId { get; private set; }
     public virtual ProductUom UomRequested { get; set; }
     public virtual ProductUom UomSpecified { get; set; }
}

public class ProductUom
{
    public virtual Guid ProductId { get; private set; }
    public virtual decimal UomType { get; set; }
}

基本上,我有 2 个对 ORDER_LINE 内的 PROD_UOM 表的引用 类。我第一次尝试映射此关系如下:

public OrderLineMap()
{
    Table("ORDER_LINE");
    Id(x => x.OrderLineId, "ORDER_LINE_ID");

    References(x => x.UomSpecified)
        .Columns(new string[] { "PROD_ID", "UOM_SPECIFIED" });

    References(x => x.UomRequested)
        .Columns(new string[] { "PROD_ID", "UOM_TYPE_REQUESTED" });
}

此映射遇到可怕的错误(PROD_ID 被引用两次):

System.IndexOutOfRangeException: Invalid index 17 for this SqlParameterCollection with Count=17. 

是否有一种简单的方法来映射此关系?

I have the following database tables (These are abbreviated for clarity):

CREATE TABLE [dbo].[prod_uom](
    [prod_id] [dbo].[uid] NOT NULL,       --Primary key
    [uom_type] [numeric](9, 0) NOT NULL,  --Primary Key
)

CREATE TABLE [dbo].[order_line](
    [order_line_id] [dbo].[uid] NOT NULL,  --Primary key
    [prod_id] [dbo].[uid] NOT NULL,        --Foreign key
    [uom_type_requested] [numeric](9, 0) NOT NULL, --Foreign key
    [uom_specified] [numeric](9, 0) NOT NULL        --Foreign key
)

Entity I'm having trouble with looks like this (Abbreviated):

public class OrderLine
{
     public virtual Guid OrderLineId { get; private set; }
     public virtual ProductUom UomRequested { get; set; }
     public virtual ProductUom UomSpecified { get; set; }
}

public class ProductUom
{
    public virtual Guid ProductId { get; private set; }
    public virtual decimal UomType { get; set; }
}

Basically I have 2 References to the PROD_UOM table inside my ORDER_LINE class. My first attempt at mapping this was the following:

public OrderLineMap()
{
    Table("ORDER_LINE");
    Id(x => x.OrderLineId, "ORDER_LINE_ID");

    References(x => x.UomSpecified)
        .Columns(new string[] { "PROD_ID", "UOM_SPECIFIED" });

    References(x => x.UomRequested)
        .Columns(new string[] { "PROD_ID", "UOM_TYPE_REQUESTED" });
}

This mapping gets the dreaded error (PROD_ID is referenced twice):

System.IndexOutOfRangeException: Invalid index 17 for this SqlParameterCollection with Count=17. 

Is there an easy way to map this relationship?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

且行且努力 2024-12-08 05:02:59

我相当确定您不能将同一列映射两次,因此我的建议是将 ORDER_LINE 中的 PROD_ID 列拆分为两个:PROD_ID_SPEC 和 PROD_ID_REQ。

还有其他一些选择,但这就是我建议的选择,因为它们可能比这更不优雅。

I'm fairly certain you can't map the same column twice so my suggestion would be to split the PROD_ID column in ORDER_LINE into two, PROD_ID_SPEC and PROD_ID_REQ.

There are some other options but that is the one I would suggest as they are probably even more inelegant than that.

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