title: Initializer.Seed() throws exception on context.SaveChanges()
body: <p>I'm relatively new to EntityFramework (started using it today!)  </p>
  
  <p>I have the following code:</p>
  
  <pre><code>    public class DemoContext : DbContext
      {
          public DemoContext() : base("demoContext")
          {
  
          }
  
          public DbSet&lt;BaseUser&gt; Users {get; set;}
          public DbSet&lt;BaseSession&gt; Sessions {get;set;} 
       }
  
      public class DemoInitializer : DropCreateDatabaseAlways&lt;DemoContext&gt;
      {
          protected override void Seed(DemoContext context)
          {
              var staffUsers = new List&lt;StaffUser&gt;
              {
                  new StaffUser {Id=1,Username="test",Password="test",DisplayName="test user",AccessFlags=(AccessFlags)2048 },
              };
  
              staffUsers.ForEach(su =&gt; context.Users.Add(su));
              context.SaveChanges();
          }
      }
  </code></pre>
  
  <p>But whenever the <code>context.SaveChanges();</code> line is called, it throws the following exception:</p>
  
  <blockquote>
    <p>An exception of type
    'System.Data.Entity.Infrastructure.DbUpdateException' occurred in
    EntityFramework.dll but was not handled in user code</p>
    
    <p>Additional information: Error retrieving values from ObjectStateEntry.
    See inner exception for details.</p>
  </blockquote>
  
  <p>And the inner exception is:</p>
  
  <blockquote>
    <p>The given key was not present in the dictionary.</p>
  </blockquote>
  
  <p>Whilst this error doesn't really mean much to me at this point, and Googling around hasn't produced any answers,  I have seemingly managed to nail it down to being related to the public property <code>DbSet&lt;BaseSession&gt; Sessions {get;set}</code> on the DemoContext class
  (By this I mean, if I comment it out, the error doesn't occur and away we go!)</p>
  
  <p>Why is this, and what's the correct fix?  </p>
  
  <p>Do I need to do something with all of the DbSet properties present in a DbContext if I use Initializer.Seed()?<br>
  <em>I'm assuming you don't actually have to populate them all with data, as that doesn't make sense?</em></p>
  
  <p>.<br>
  Screenshot of the full exception, incase it's useful!
  <img src="https://i.stack.imgur.com/OPl7P.png" alt="Screenshot of the full exception, incase it&#39;s useful!"></p>
  
  <p>.<br>
  <strong>EDIT</strong></p>
  
  <p>I've dialed it down to being a problem with this specific implementation of the <code>BaseSession</code> class:</p>
  
  <pre><code>public class UniqueSession : BaseSession
  {
       public Date DateOfSession { get; set; }
       public string Comments { get; set; }
  }
  </code></pre>
  
  <p>Which, as you can see, uses a custom <code>Date</code> class, which I pilfered from another question on here:</p>
  
  <pre><code>    public class Date : IEquatable&lt;Date&gt;, IEquatable&lt;DateTime&gt;
      {
          public Date(DateTime date)
          {
              value = new DateTime(date.Date.Ticks, DateTimeKind.Unspecified);
              //value = date.Date;
          }
  
          public bool Equals(Date other)
          {
              return other != null &amp;&amp; value.Equals(other.value);
          }
  
          public bool Equals(DateTime other)
          {
              return value.Equals(other);
          }
  
          public override string ToString()
          {
              return value.ToString();
          }
          public static implicit operator DateTime(Date date)
          {
              return date.value;
          }
          public static explicit operator Date(DateTime dateTime)
          {
              return new Date(dateTime);
          }
  
          private DateTime value;
      }
  </code></pre>
  
  <p>If I switch <code>UniqueSession.DateOfSession</code> to be a <code>DateTime</code>, this error doesn't occur?</p>
  
  <p>.<br>
  <strong>EDIT 2</strong><br>
  If I change the <code>Date</code> class so that the <code>private DateTime value;</code> is actually a public property (<code>public DateTime value { get; set; }</code>), the error goes away!</p>
  
  <p>But why?</p>