开发者

Code First CTP4 for EF4 - how to map multiple entities (common base) to single table

开发者 https://www.devze.com 2023-01-14 20:46 出处:网络
I have a large table that I want to map to a few entities. Say the table looks like this: Thing(ThingId, Property1...Property20)

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 : ThingB开发者_如何学Pythonase
{
    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?


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.

0

精彩评论

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