【问题标题】:Mapping an weak relationship to an ORM将弱关系映射到 ORM
【发布时间】:2012-04-23 12:32:42
【问题描述】:

考虑这 3 个表格:

Client
  - Id

Product
  - Id

Invoice_Row
  - Id
  - LinkedObjectId
  - LinkedObjectType

Invoice_row 是指上面使用 LinkedObjectId 和 LinkedObjectType 字段的表格,例如:

Invoice_row ID = 1
  - LinkedObjectId = 1 (meaning product id = 1)
  - LinkedObjectType = 'Product'

Invoice_row  ID = 2
  - LinkedObjectId = 1 (meaning client id = 1)
  - LinkedObjectType = 'Client'

如您所见,invoice_row 对另外两个表有一个动态键(意味着它实际上不是键)。它通过首先查看 LinkedObjectType(要引用哪个表),然后是 LinkedObjectId(引用表中的特定 ID)来引用其他两个表。

是的,我知道这很糟糕,但这是我必须使用的遗留数据模型。

有什么方法可以以正常方式(使用 NH 或 EF)以某种方式映射,如下所示:

Client
    Invoice_Rows
Product
    Invoice_Rows

谢谢!

【问题讨论】:

  • 正常映射是什么意思?你想检索和保存实体?我不相信有任何方法可以保存实体。
  • 我只想检索实体,不关心保存。

标签: entity-framework nhibernate orm


【解决方案1】:

在 NHibernate 中(使用 Fluentmapping)

class Invoice
{
    public virtual IHasInvoices Owner { get; set; }
    // or
    public virtual object Owner { get; set; }
}

// in InvoiceMap
ReferencesAny(x => x.Owner)
    .EntityIdentifierColumn("LinkedObjectId")
    .EntityTypeColumn("LinkedObjectType")
    .IdentityType<long>()
    .AddMetaValue<Product>("product")
    .AddMetaValue<Client>("client");

那么你就可以把它当作简单的关联来使用(除了你只能设置映射的实体)

【讨论】:

    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 2013-09-02
    • 2011-04-15
    相关资源
    最近更新 更多