【问题标题】:Code First CTP4 for EF4 - how to map multiple entities (common base) to single tableCode First CTP4 for EF4 - 如何将多个实体(公共基础)映射到单个表
【发布时间】:2010-09-01 17:13:36
【问题描述】:

我有一张大表,想映射到几个实体。

假设表格如下所示:Thing(ThingId, Property1...Property20)

现在我有了我的实体:

public abstract class ThingBase
{
    public int ThingId { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

public class ThingSummary : ThingBase
{
    public string Property3 { get; set; }    
}

public class Thing : ThingBase
{
    public string Property3 { get; set; }
    //...
    public string Property20 { get; set; }
}

如何设置我的 DbContext 以使其正常工作?我有:

public DbSet<ThingSummary> ThingSummaries { get; set; }

public DbSet<Thing> Things { get; set; }

但我收到错误“无效的对象名称'dbo.ThingSummaries'。”当我尝试查询时。

我已尝试添加到 OnModelCreating:

modelBuilder.Entity<ThingSummary>().MapSingleType().ToTable("Things");

但这似乎没有任何作用。

有什么想法吗?

【问题讨论】:

    标签: entity-framework entity-framework-4 ef4-code-only


    【解决方案1】:

    我认为您不能拥有 ThingSummaries。你只能拥有事物。您也不能使用 MapSingleType 因为它说只有单一类型将映射到表。您必须改用 MapHearchy。我在question 中发布了此类映射的示例。

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 2012-04-21
      • 2015-01-07
      • 2013-10-19
      • 2022-01-23
      相关资源
      最近更新 更多