title: Is it possible to access a shared TPH column in EF Core without using intermediate classes?
body: <p>When using <a href="https://docs.microsoft.com/en-us/ef/core/modeling/inheritance#shared-columns" rel="nofollow noreferrer">shared columns</a> in an EF Core TPH setup, is it possible to <strong>access the shared column during projection</strong>?</p>
  <pre class="lang-cs prettyprint-override"><code>    class Program
      {
          public static readonly ILoggerFactory MyLoggerFactory
              = LoggerFactory.Create(builder =&gt; { 
                  builder.AddConsole(); 
              });
  
          static async Task Main(string[] args)
          {
              using (var context = new ClientContext())
              {
                  context.Database.EnsureDeleted();
                  context.Database.EnsureCreated();
  
                  var actions = await context.Actions
                      .Select(a =&gt; new
                      {
                          Id = a.Id,
  
                          // this works - but really messy and complex in real world code
                          Message = (a as ActionA).Message ?? (a as ActionB).Message,
  
                          // this throws &quot;Either the query source is not an entity type, or the specified property does not exist on the entity type.&quot;
                          // is there any other way to access the shared column Message?
                          // Message = EF.Property&lt;string&gt;(a, &quot;Message&quot;),
                      })
                      .ToListAsync();
  
                  actions.ForEach(a =&gt; Console.WriteLine(a.Id + a.Message));
              }
          }
  
          public class ActionBase
          {
              public int Id { get; set; }
  
              // ... other shared properties
          }
  
          public class ActionA : ActionBase
          {
              // shared with B
              [Required]
              [Column(&quot;Message&quot;)]
              public string Message { get; set; }
  
              // ... other specific properties
          }
  
          public class ActionB : ActionBase
          {
              // shared with A
              [Required]
              [Column(&quot;Message&quot;)]
              public string Message { get; set; }
  
              // ... other specific properties
          }
  
          public class ActionC : ActionBase
          {
              public string SomethingElse { get; set; }
  
              // ... other specific properties
          }
  
          class ClientContext : DbContext
          {
              protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
              {
                  // TO USE SQL
                  //optionsBuilder
                  //    .UseLoggerFactory(MyLoggerFactory)
                  //    .UseSqlServer(&quot;Server=(localdb)\\mssqllocaldb;Database=TPHSharedColumn;Trusted_Connection=True;MultipleActiveResultSets=true;Connect Timeout=30&quot;)
                  //    .EnableSensitiveDataLogging(false);
                  // TO USE INMEMORY
                  optionsBuilder
                     .UseLoggerFactory(MyLoggerFactory)
                     .UseInMemoryDatabase(Guid.NewGuid().ToString());
              }
  
              protected override void OnModelCreating(ModelBuilder builder)
              {
                  base.OnModelCreating(builder);
  
                  builder.Entity&lt;ActionA&gt;().HasData(new ActionA()
                  {
                      Id = 1,
                      Message = &quot;A&quot;
                  });
                  builder.Entity&lt;ActionB&gt;().HasData(new ActionB()
                  {
                      Id = 2,
                      Message = &quot;B&quot;
                  });
                  builder.Entity&lt;ActionC&gt;().HasData(new ActionC()
                  {
                      Id = 3,
                      SomethingElse = &quot;C&quot;
                  });
              }
  
              public DbSet&lt;ActionBase&gt; Actions { get; set; }
          }
      }
  </code></pre>
  <p>In this simple example, it would of course be possible to move <code>Message</code> to the base class - but that would make it possible to accidentally add an <code>ActionC</code> with a <code>Message</code> since I would need to remove the <code>Required</code> attribute.</p>
  <p>I also know I could add a <code>ActionWithRequiredMessage</code> intermediate class to inherit <code>ActionA</code> and <code>ActionB</code> with, but again - in the much more complex real world example this is not feasible since there are also other shared columns and C# does not allow inheriting from multiple classes - and EF Core does not seem to like to use interfaces for this.</p>
  <p>I simply would like to find a way to <strong>directly access the shared column</strong> - and use it in a projection.</p>
  <p>Anyone know if this is possible?</p>