【问题标题】:Can I map an EF entity to two tables with a polymorphic foreign key?我可以使用多态外键将 EF 实体映射到两个表吗?
【发布时间】:2011-01-25 19:33:59
【问题描述】:

我想创建一个新的映射实体,如下所示:

public class PathedItem
{
  public long Id { get; set; }      // from the Items table
  public string Name { get; set; }  // from the Items table
  public string Path { get; set; }  // from the Paths table
}

问题在于Path 与其他项目位于不同的表中,并且其中一个表具有多态外键。这是我的表格:

CREATE TABLE Items (
  [Id] [bigint] IDENTITY(1,1) NOT NULL,
  [Name] [nvarchar](255) NOT NULL)

CREATE TABLE Paths (
  [Id] [bigint] IDENTITY(1,1) NOT NULL,
  [Path] [nvarchar](255) NOT NULL,
  [ItemId] [bigint] NOT NULL,
  [ItemType] [int] NOT NULL)

Microsoft 有关于将实体映射到两个表(herehere)的 HOWTO,但它们似乎依赖于普通的外键。

有没有办法将Paths.ItemId 映射到Items.Id,然后在连接中硬编码Paths.ItemType 的值?

【问题讨论】:

    标签: .net entity-framework entity-framework-4 polymorphic-associations edmx


    【解决方案1】:

    执行此操作的一种方法是创建一个视图,其中包含您需要的列和 ItemType 上的过滤器。

    然后将该视图添加到您的实体模型中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      相关资源
      最近更新 更多