title: How to use abstract class in Entity Framework Core?
body: <p>Starting out with EF Core, I'm trying to use an abstract class. I understand that I can't instantiate an abstract class and have a part of code missing but cannot find how to solve it.</p>
  <p>The code is as follows:</p>
  <pre><code>using System;
  using Microsoft.EntityFrameworkCore;
  using System.ComponentModel.DataAnnotations;
  using Microsoft.Extensions.Logging;
  
  namespace ProjectTest1 
  {
      public abstract class User 
      {
          [Key]
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string Password { get; set; }
          public string Email { get; set; }
          public bool IsAdmin { get; set; }
      }
  
      public class Student : User 
      {
          public string Number { get; set; }
      }
  
      public class Teacher : User 
      {
          public int Salary { get; set; }
      }
  
      public class Model : DbContext 
      {
          public static readonly ILoggerFactory _loggerFactory = LoggerFactory.Create(builder =&gt; {
              builder.AddConsole();
          });
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
              base.OnConfiguring(optionsBuilder);
              optionsBuilder.UseSqlServer(@&quot;Server=(localdb)\mssqllocaldb;Database=project&quot;)
                  .EnableSensitiveDataLogging()
                  .UseLoggerFactory(_loggerFactory);
          }
  
          public DbSet&lt;User&gt; Users { get; set; }
      }
  
      class Program 
      {
          static void Main(string[] args) 
          {
              using var model = new Model();
              model.Database.EnsureCreated();
  
              model.Users.RemoveRange(model.Users);
              model.SaveChanges();
  
              var guillaume = new Student() 
              {
                  FirstName = &quot;guillaume&quot;, 
                  LastName = &quot;b&quot;, 
                  Password = &quot;azerty&quot;,
                  Email = &quot;guillaume@gmail.com&quot;, 
                  IsAdmin = true,
                  Number = &quot;150200&quot;
              };
  
              var donald = new Teacher() 
              {
                  FirstName = &quot;donald&quot;, 
                  LastName = &quot;c&quot;, 
                  Password = &quot;azerty&quot;,
                  Email = &quot;donald@gmail.com&quot;, 
                  IsAdmin = false, 
                  Salary = 35000
              };
  
              model.Users.AddRange(new User[] { guillaume, donald });
              model.SaveChanges();
          }
      }
  }
  </code></pre>
  <p>The execution of the code produce error:</p>
  <blockquote>
  <p>System.InvalidOperationException<br />
  HResult=0x80131509</p>
  <p>Message=The corresponding CLR type for entity type 'User' cannot be instantiated, and there is no derived entity type in the model that corresponds to a concrete CLR type.</p>
  <p>Source=Microsoft.EntityFrameworkCore</p>
  <p>StackTrace:</p>
  <p>at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateClrInheritance(IModel model, IEntityType entityType, HashSet<code>1 validEntityTypes)   at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateClrInheritance(IModel model, IDiagnosticsLogger</code>1 logger)
  at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger<code>1 logger) at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger</code>1 logger)
  at Microsoft.EntityFrameworkCore.SqlServer.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger<code>1 logger) at Microsoft.EntityFrameworkCore.Metadata.Conventions.ValidatingConvention.ProcessModelFinalized(IModel model) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelFinalized(IModel model) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelFinalized(IModel model) at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FinalizeModel() at Microsoft.EntityFrameworkCore.ModelBuilder.FinalizeModel() at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model() at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.&lt;&gt;c.&lt;TryAddCoreServices&gt;b__7_3(IServiceProvider p) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor</code>2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<code>2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.&lt;&gt;c__DisplayClass1_0.&lt;RealizeService&gt;b__0(ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure&lt;System.IServiceProvider&gt;.get_Instance() at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure</code>1 accessor)
  at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
  at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_Dependencies()
  at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
  at ProjectTest1.Program.Main(String[] args) in C:\Users\yamab\Desktop\ProjectTest1\ProjectTest1\Program.cs:line 48</p>
  </blockquote>
  <p>Reading this <a href="https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph" rel="nofollow noreferrer">tutorial</a> I think I should do something like this</p>
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;User&gt;()
                  .Map&lt;Studentt&gt;(m =&gt; m.Requires(&quot;User&quot;).HasValue(&quot;S&quot;))
                  .Map&lt;Teacher&gt;(m =&gt; m.Requires(&quot;Usre&quot;).HasValue(&quot;T&quot;));
  }
  </code></pre>
  <p>Not sure this is what I should do nor how to do it exactly?</p>