在同一个类中映射两个引用,但引用类具有复合键 [英] Mapping two references in the same class but reference class has Composite key

查看:143
本文介绍了在同一个类中映射两个引用,但引用类具有复合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ $ $ $ $ codeREATE TABLE [dbo]。[prod_uom] (
[prod_id] [dbo]。[uid] NOT NULL, - 主键
[uom_type] [数字](9,0)非NULL, - 主键


CREATE TABLE [dbo]。[order_line](
[order_line_id] [dbo]。[uid] NOT NULL,--Primary key
[prod_id] [dbo]。[ uid] NOT NULL, - 外键
[uom_type_requested] [数字](9,0)NOT NULL, - 外键
[uom_specified] [数字](9,0)外键





$ b

实体我遇到了这样的问题(缩写):

  public class OrderLine 
{
public virtual Guid OrderLineId {get;私人设置}
public virtual ProductUom UomRequested {get;组; }
public virtual ProductUom UomSpecified {get;组; }
}

public class ProductUom
{
public virtual Guid ProductId {get;私人设置}
public virtual decimal UomType {get;组; } b



$ p $基本上我有两个对 PROD_UOM 的引用我的 ORDER_LINE 类中的表。我的第一次尝试映射如下:

pre $ public $ Order $ $
$ $ b $ ORDER_LINE);
Id(x => x.OrderLineId,ORDER_LINE_ID);

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

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



$ b

这个映射得到了可怕的错误(PROD_ID被引用两次):

  System.IndexOutOfRangeException:此Count = 17的SqlParameterCollection的索引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?

解决方案

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.

这篇关于在同一个类中映射两个引用,但引用类具有复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆