Code First CTP4 for EF4 - 如何将多个实体(公共基础)映射到单个表
我有一个大表,我想将其映射到几个实体。
假设表格如下所示: 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");
但这似乎没有做任何事情。
有什么想法吗?
I have a large table that I want to map to a few entities.
Say the table looks like this: Thing(ThingId, Property1...Property20)
Now I have my entities:
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; }
}
How do I set up my DbContext so it will work? I had:
public DbSet<ThingSummary> ThingSummaries { get; set; }
public DbSet<Thing> Things { get; set; }
but I get an error "Invalid object name 'dbo.ThingSummaries'." when I try to query.
I have tried adding to OnModelCreating:
modelBuilder.Entity<ThingSummary>().MapSingleType().ToTable("Things");
but this did not seem to do anything.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不能拥有 ThingSummaries。你只能拥有东西。您也不能使用 MapSingleType,因为它表示只有单一类型才会映射到表。您必须使用 MapHiearchy 来代替。我在此问题中发布了此类映射的示例。
I think you can't have ThingSummaries. You can only have Things. You also can't use MapSingleType because it says that only single type will be mapped to the table. You have to use MapHiearchy instead. I posted example of such mapping in this question.