title: Entity Framework Code First: NULL values inserted in database
body: <p>I am using Entity Framework Code First Approach. I have following code to insert data into PaymentComponent and Payment tables. The data getting inserted into PaymentComponent table is not proper. It has NULL values in two columns (for one record) even though the corresponding properties in the domain objects are not null. What need to be changed in order to make it working?</p>
  
  <p><img src="https://i.stack.imgur.com/6WyU3.jpg" alt="enter image description here"></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>When I added the following in NerdDinners class, I am getting following result - it has new unwanted columns</p>
  
  <pre><code>  public DbSet&lt;ClubCardPayment&gt; ClubCardPayments { get; set; }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ilwKq.jpg" alt="enter image description here"></p>
  
  <p>ORIGINAL CODE</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>DOMAIN CODE</p>
  
  <pre><code>public abstract class PaymentComponent
  {
      public int PaymentComponentID { get; set; }
      public abstract int MyValue { get; set; }
      public abstract string MyType { get; set; }
      public abstract int GetEffectiveValue();
  }
  
  
  public partial class GiftCouponPayment : PaymentComponent
  {
  
      private int couponValue;
      private string myType;
  
      public override int MyValue
      {
          get
          {
              return this.couponValue;
          }
          set
          {
              this.couponValue = value;
          }
      }
  
      public override string MyType
      {
          get
          {
              return this.myType;
          }
          set
          {
              this.myType = value;
          }
      }
  
      public override int GetEffectiveValue()
      {
          if (this.PaymentComponentID &lt; 2000)
          {
              return 0;
          }
          return this.couponValue;
      }
  
  }
  
  
  public partial class ClubCardPayment : PaymentComponent
  {
  
      private int cardValue;
      private string myType;
  
      public override int MyValue
      {
          get
          {
              return this.cardValue;
          }
          set
          {
              this.cardValue = value;
          }
      }
  
      public override string MyType
      {
          get
          {
              return this.myType;
          }
          set
          {
              this.myType = value;
          }
      }
  
      public override int GetEffectiveValue()
      {
          return this.cardValue;
      }
  
  }
  
  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;Payment&gt; Payments { get; set; }
  
  }
  </code></pre>
  
  <p><strong>REFERENCE</strong>:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/9434082/when-using-entity-framework-code-first-mapping-property-to-separate-table-moves">When using entity framework code-first mapping property to separate table, moves foreign key field</a></li>
  <li><a href="https://stackoverflow.com/questions/5689696/override-entity-framework-entity-property">Override Entity Framework Entity Property</a></li>
  <li><a href="https://stackoverflow.com/questions/2511357/entityframework-how-to-override-properties">EntityFramework how to Override properties</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>