title: Entity Framework 6.1 Discriminator TPH
body: <p>I am looking to implement Table-per-Hierarchy using EF6 similar to the instructions found here:
  <a href="http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph#7667933" rel="nofollow">example</a>.</p>
  
  <p>I have an abstract base class of <em>User</em> with the following derived types:</p>
  
  <ul>
  <li>Student</li>
  <li>Contact</li>
  <li>Instructor</li>
  </ul>
  
  <p>When I examine the database table <em>Users</em> the <em>discriminator</em> column value is <strong>(Undefined)</strong> when I pass a student object into my <em>Save</em> method below.  Instead I would expect the value to be <em>Student</em>.  Otherwise my data is saved correctly in both the <em>Users</em> and <em>Students</em> tables.  </p>
  
  <p>While troubleshooting the problem I added a UserType enumerator Get property to the classes to ensure that I am casting from <em>User</em> to <em>Student</em>.</p>
  
  <p>In my <em>UserRepository</em> class my <em>Save</em> method is below.</p>
  
  <pre><code>    public void Save(User user)
      {
        if (Exists(user.Id))
          UpdateUser(user);
        else
        {
          switch (user.Role)
          {
            case UserType.Role.Base:
             _db.Users.Add(user);
              break;
            case UserType.Role.Student:
             _db.Users.Add(user as Student);
             break;
           case UserType.Role.Instructor:
             _db.Users.Add(user as Instructor);
             break;
           case UserType.Role.Contact:
             _db.Users.Add(user as Contact);
             break;
         }
       }
       _db.SaveChanges();
      }
  </code></pre>
  
  <h1>Failed Alternative</h1>
  
  <p>I've tried code like the following to explicitly create a new <em>Student</em>.</p>
  
  <pre><code>    private void MapToStudent(User user)
      {
       _db.Users.Add(new Student()
       {
         FirstName = user.FirstName,
         LastName = user.LastName,
         //...
        });
      }
  </code></pre>
  
  <h1>Question</h1>
  
  <p>I am not downcasting correctly?  Or rather what is the proper/preferred way to save subclasses using EF?</p>
  
  <h1>User Base Class</h1>
  
  <pre><code>      public abstract class User
        {
           public int Id { get; set; }
           //...
        }
  
        internal class UserNotFound: User 
        { 
          public override UserType.Role Role 
          { 
            get 
              { 
               return UserType.Role.Base; 
              } 
          }
        }
  
        public class Student : User 
        {
           //...
           public override UserType.Role Role 
           {
             get { return UserType.Role.Student; }          
           }
        }
  
        public class Contact : User 
        {
           //...
           public override UserType.Role Role 
           {
             get { return UserType.Role.Contact; }          
           }
        }
  
        public class Instructor : User 
        {
           //...
           public override UserType.Role Role 
           {
             get { return UserType.Role.Instructor; }          
           }
        }
  </code></pre>
  
  <h1>DatabaseContext Mapping</h1>
  
  <pre><code>      public class DatabaseContext : Context
        {
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity&lt;Student&gt;().ToTable("Students");
                modelBuilder.Entity&lt;Contact&gt;().ToTable("Contacts");
                modelBuilder.Entity&lt;Instructor&gt;().ToTable("Instructors");
            }
        }
  </code></pre>