映射同一类中的两个引用,但引用类具有复合键
我有以下数据库表(为了清楚起见,这些表已缩写):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相当确定您不能将同一列映射两次,因此我的建议是将 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.