locked
Code first: how to implement table per type RRS feed

  • Question

  • Hi all,

    it seems I cannot find how to make table per type inheritance work with "code first".

    I have (simplified):

    public abstract class Project : BaseEntity
    {
     public int Id {get; set; }
     public int Name {get; set; }
    }
    
    public class KitchenProject : Project
    
    { 
     public string SomeProperty {get; set; }
    }
    

    and the database tables are:

    CREATE TABLE [dbo].[Project]
    (
    	[Id] [int] IDENTITY(1,1) NOT NULL,
    	[ProjectName] [nvarchar](50) NOT NULL,		
    )
    
    CREATE TABLE [dbo].[KitchenProject]
    (
    	[ProjectId] [int] IDENTITY(1,1) NOT NULL,	
    	[SomeProperty] [nvarchar](200) NOT NULL,	
    )
    
    
    
    Can anyone help me to map this?

     

     

     

     

     

     

     

     

     

    Tuesday, October 12, 2010 8:11 AM

Answers

  • Hi,

    There are examples of the various inheritance mapping here; http://romiller.com/2010/09/29/ef-ctp4-tips-tricks-code-first-inheritance-mapping/

    Here is what your mapping would look like, note that in CPT4 you will need to remove 'abstract' from Project to work around a bug:

    public class ProjectContext : DbContext
    {
      public DbSet<Project> Projects { get; set; }
    
      protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
      {
        modelBuilder.Entity<Project>()
            .MapHierarchy(p => new
            {
              p.Id,
              ProjectName = p.Name
            })
            .ToTable("dbo.Project");
    
        modelBuilder.Entity<KitchenProject>()
          .MapHierarchy(d => new
          {
            ProjectId = d.Id,
            d.SomeProperty
          })
          .ToTable("dbo.KitchenProject");
      }
    }
    

    ~Rowan

    • Proposed as answer by Rowan MillerModerator Wednesday, October 13, 2010 6:14 AM
    • Marked as answer by LudwigSt Wednesday, October 13, 2010 11:50 AM
    Wednesday, October 13, 2010 6:14 AM
    Moderator

All replies

  • Hi,

    There are examples of the various inheritance mapping here; http://romiller.com/2010/09/29/ef-ctp4-tips-tricks-code-first-inheritance-mapping/

    Here is what your mapping would look like, note that in CPT4 you will need to remove 'abstract' from Project to work around a bug:

    public class ProjectContext : DbContext
    {
      public DbSet<Project> Projects { get; set; }
    
      protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
      {
        modelBuilder.Entity<Project>()
            .MapHierarchy(p => new
            {
              p.Id,
              ProjectName = p.Name
            })
            .ToTable("dbo.Project");
    
        modelBuilder.Entity<KitchenProject>()
          .MapHierarchy(d => new
          {
            ProjectId = d.Id,
            d.SomeProperty
          })
          .ToTable("dbo.KitchenProject");
      }
    }
    

    ~Rowan

    • Proposed as answer by Rowan MillerModerator Wednesday, October 13, 2010 6:14 AM
    • Marked as answer by LudwigSt Wednesday, October 13, 2010 11:50 AM
    Wednesday, October 13, 2010 6:14 AM
    Moderator
  • Hi,

    There are examples of the various inheritance mapping here; http://romiller.com/2010/09/29/ef-ctp4-tips-tricks-code-first-inheritance-mapping/

    Here is what your mapping would look like, note that in CPT4 you will need to remove 'abstract' from Project to work around a bug:

    public class ProjectContext : DbContext
    {
     public DbSet<Project> Projects { get; set; }
    
     protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
     {
      modelBuilder.Entity<Project>()
        .MapHierarchy(p => new
        {
         p.Id,
         ProjectName = p.Name
        })
        .ToTable("dbo.Project");
    
      modelBuilder.Entity<KitchenProject>()
       .MapHierarchy(d => new
       {
        ProjectId = d.Id,
        d.SomeProperty
       })
       .ToTable("dbo.KitchenProject");
     }
    }
    

    ~Rowan

    That works indeed, it was the abstract that caused problems! THANKS!
    Wednesday, October 13, 2010 11:51 AM