Mapping two references in the same class but reference class has Composite key

六月ゝ 毕业季﹏ 提交于 2019-12-23 12:43:42

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7167516/mapping-two-references-in-the-same-class-but-reference-class-has-composite-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!