Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Check the section about TPH in this <a href="http://blogs.msdn.com/b/efdesign/archive/2009/10/12/code-only-further-enhancements.aspx" rel="nofollow noreferrer">article</a>. If Mission and Column are complex types you will also find there how to map them. Generally you have to use MapHiearchy and Case methods instead of MapSingleType.</p> <p><strong>Edit:</strong> </p> <p>Here is the example:</p> <pre><code>using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; namespace EFTest { public class RequestBase { public int Id { get; set; } public DateTime? RequestedDate { get; set; } public int UserId { get; set; } } public class Mission { public int Id { get; set; } public string Name { get; set; } public virtual ICollection&lt;MissionRequest&gt; MissionRequests { get; set; } } public class Column { public string Name { get; set; } } public class MissionRequest : RequestBase { public virtual Mission Mission { get; set; } } public class ShiftRequest : RequestBase { public Column Column { get; set; } } public class TestContext : DbContext { public DbSet&lt;RequestBase&gt; Requests { get; set; } public DbSet&lt;Mission&gt; Missions { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ContainerName = "EFTest"; modelBuilder.IncludeMetadataInDatabase = false; // Example of complex type mapping. First you have to define // complex type. Than you can access type properties in // MapHiearchy. var columnType = modelBuilder.ComplexType&lt;Column&gt;(); columnType.Property(c =&gt; c.Name).HasMaxLength(50); modelBuilder.Entity&lt;Mission&gt;() .Property(m =&gt; m.Id) .IsIdentity(); modelBuilder.Entity&lt;Mission&gt;() .HasKey(m =&gt; m.Id) .MapSingleType(m =&gt; new { m.Id, m.Name }) .ToTable("dbo.Missions"); modelBuilder.Entity&lt;RequestBase&gt;() .Property(r =&gt; r.Id) .IsIdentity(); // You map multiple entities to single table. You have to // add some discriminator to differ entity type in the table. modelBuilder.Entity&lt;RequestBase&gt;() .HasKey(r =&gt; r.Id) .MapHierarchy() .Case&lt;RequestBase&gt;(r =&gt; new { r.Id, r.RequestedDate, r.UserId, Discriminator = 0 }) .Case&lt;MissionRequest&gt;(m =&gt; new { MissionId = m.Mission.Id, Discriminator = 1 }) .Case&lt;ShiftRequest&gt;(s =&gt; new { ColumnName = s.Column.Name, Discriminator = 2 }) .ToTable("dbo.Requests"); } } } </code></pre> <p><strong>Edit 2:</strong></p> <p>I updated example. Now Mission is entity instead of complex type.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload