开发者

Can I map an EF entity to two tables with a polymorphic foreign key?

I would like to make a new, mapped entity that looks like this:

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
}

The problem is that Path is in a different table than the other items and one of those tables has a polymorphic foreign key. Here are my tables:

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 has HOWTOs on mapping entities开发者_如何学Go to two tables (here and here), but they seem to rely on a normal foreign key.

Is there some way to map Paths.ItemId to Items.Id and then hardcode a value for Paths.ItemType in the join?


One way to do this would be to create a view, with the columns that you require and a filter on the ItemType.

Then add that view to your entity model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜