title: Entity Framework: Split table into multiple tables
body: <p>I have the following table [PaymentComponent] created using following EF code first approach (TPH inheritance). It works fine. I need to change the database design – need to store GiftCouponPayments in GiftCouponPayment table and ClubCardPayments in ClubCardPayment table. What change need to be done in C# code to get the required database structure?</p>
  
  <p><img src="https://i.stack.imgur.com/MGEiA.jpg" alt="enter image description here"></p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code>public abstract class PaymentComponent
  {
      public int PaymentComponentID { get; set; }
      public int MyValue { get; set; }
      public string MyType { get; set; }
      public abstract int GetEffectiveValue();
  }
  
  
  public partial class GiftCouponPayment : PaymentComponent
  {
  
      public override int GetEffectiveValue()
      {
          if (MyValue &lt; 2000)
          {
              return 0;
          }
          return MyValue;
      }
  
  }
  
  
  public partial class ClubCardPayment : PaymentComponent
  {
      public override int GetEffectiveValue()
      {
          return MyValue;
      }
  }
  
  public partial class Payment
  {
      public int PaymentID { get; set; }
      public List&lt;PaymentComponent&gt; PaymentComponents { get; set; }
      public DateTime PayedTime { get; set; }
  
  }
  
  
  
  //System.Data.Entity.DbContext is from EntityFramework.dll
  public class NerdDinners : System.Data.Entity.DbContext
  {
  
      public NerdDinners(string connString): base(connString)
      { 
  
      }
  
      protected override void OnModelCreating(DbModelBuilder modelbuilder)
      {
          modelbuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
      }
  
  
      public DbSet&lt;GiftCouponPayment&gt; GiftCouponPayments { get; set; }
      public DbSet&lt;ClubCardPayment&gt; ClubCardPayments { get; set; }
      public DbSet&lt;Payment&gt; Payments { get; set; }
  
  }
  </code></pre>
  
  <p><strong>CLIENT</strong></p>
  
  <pre><code>static void Main(string[] args)
  {
      string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";
  
      using (var db = new NerdDinners(connectionstring))
      {
          GiftCouponPayment giftCouponPayment = new GiftCouponPayment();
          giftCouponPayment.MyValue=250;
          giftCouponPayment.MyType = "GiftCouponPayment";
  
          ClubCardPayment clubCardPayment = new ClubCardPayment();
          clubCardPayment.MyValue = 5000;
          clubCardPayment.MyType = "ClubCardPayment";
  
          List&lt;PaymentComponent&gt; comps = new List&lt;PaymentComponent&gt;();
          comps.Add(giftCouponPayment);
          comps.Add(clubCardPayment);
  
          var payment = new Payment { PaymentComponents = comps, PayedTime=DateTime.Now };
          db.Payments.Add(payment);
  
          int recordsAffected = db.SaveChanges();
      }
  
  }
  </code></pre>
  
  <p><strong>REFERENCE</strong>:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/9511021/how-do-i-get-entity-framework-4-3-code-first-to-map-a-subclass-using-table-per-t">How do I get Entity Framework 4.3 Code First to map a subclass using Table Per Type (TPT)?</a></li>
  <li><a href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx</a></li>
  <li><a href="http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting/" rel="nofollow noreferrer">http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting/</a></li>
  <li>Entity Framework Mapping Scenarios - <a href="http://msdn.microsoft.com/en-us/library/cc716779.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/cc716779.aspx</a></li>
  <li><a href="http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/06/entity-splitting-in-entity-framework.aspx" rel="nofollow noreferrer">http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/06/entity-splitting-in-entity-framework.aspx</a></li>
  </ol>