title: Invalid cast within EF core entity data access
body: <p>I have a problem receiving an entity within my .net core service using entity framework core. 
  The problem occurs when the data is selected and leads to an exception as soon as I am trying to access my <code>DbSet</code>. </p>
  
  <p>I am trying to access my db set like this: 
  <a href="https://i.stack.imgur.com/yFniU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yFniU.png" alt="Access of the customers db set"></a></p>
  
  <p>When I check the <code>_dbContext.Customers</code> results view I see the following error:
  <a href="https://i.stack.imgur.com/lpfUJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lpfUJ.png" alt="The cast error"></a></p>
  
  <p>So it seems like some cast of the data doesn't work. What I don't understand is why this happens. I've checked my model and my database entity and everything should be fine ...</p>
  
  <p>Here's my code: 
  </p>
  
  <p>Model:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Table("customers")]
  public class Customer
  {
    [Column("id")]
    public int Id { get; set; }
    [Column("code")]
    public string Code { get; set; }
    [Column("card_num")]
    public string CardNum { get; set; }
    [Column("is_valid")]
    public bool IsValid { get; set; }
    [Column("valid_from")]
    public DateTime ValidFrom { get; set; }
    [Column("valid_to")]
    public DateTime ValidTo { get; set; }
    [Column("card_type")]
    public CardType CardType { get; set; }
    [Column("prepay_balance_cash")]
    public decimal PrepayBalanceCash { get; set; }
    [Column("prepay_limit_low")]
    public decimal PrepayLimitLow { get; set; }
    [Column("prepay_limit_high")]
    public decimal PrepayLimitHigh { get; set; }
    [Column("bc_contract_number")]
    public string BcContractNumber { get; set; }
  }
  
  public enum CardType
  {
    Credit = 1, 
    Prepayment = 2,
    PrepaymentAndCredit = 3,
    Identification = 4,
    CreditAndPrepayment = 5
  }
  </code></pre>
  
  <p>Db Context: </p>
  
  <pre class="lang-cs prettyprint-override"><code>public DbSet&lt;Customer&gt; Customers { get; set; }
  
  protected override void OnModelCreating(ModelBuilder builder)
  {
    base.OnModelCreating(builder);
    builder.Entity&lt;Customer&gt;().HasKey(c =&gt; c.Id);
  }
  </code></pre>
  
  <p>Database Table:</p>
  
  <p><a href="https://i.stack.imgur.com/NAitc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NAitc.png" alt="Database table part 1"></a></p>
  
  <p>...</p>
  
  <p><a href="https://i.stack.imgur.com/P1L6l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P1L6l.png" alt="Database table part 2"></a></p>
  
  <p>I really don't get it. I don't know why this is happening, I've got some other tables implemented the exact same way which work as expected. Just this one doesn't.</p>
  
  <p>Anybody got an idea what's wrong?</p>
  
  <p>Best regards</p>