title: EF Core MySQL - Can't map DBnull values
body: <p>I have an issue that I can't seem to get around. I'm working with renewing an application based on an existing MySQL database. I'm using EF Core 6.0.1 and <a href="https://www.nuget.org/packages/Pomelo.EntityFrameworkCore.MySql/" rel="nofollow noreferrer">Pomelo.EntityFrameworkCore.MySql 6.0.0</a>.</p>
  <p>I can easily retrieve data from tables so I know the setup is working. However - when I try to map an entity to a table containing a nullable value in MySql, EF Core throws an error:</p>
  <pre><code>System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.
  </code></pre>
  <p>A short example could be this class:</p>
  <pre><code>public class User
  {
      public int Id { get; set; }
      public string Username { get; set; }
      public string Email { get; set; };
  }
  </code></pre>
  <p>Mapped to a table in MySQL:</p>
  <pre><code>CREATE TABLE `Users` (
      `Id` INT(11) NOT NULL,
      `Username` VARCHAR(50) NULL DEFAULT NULL,
      `Email` VARCHAR(200) NULL DEFAULT NULL,
      PRIMARY KEY (`Id`) USING BTREE
  ;
  </code></pre>
  <p>I have the following mapping in place:</p>
  <pre><code>public void Configure(EntityTypeBuilder&lt;User&gt; builder)
  {
      builder.ToTable(&quot;Users&quot;);
      builder.HasKey(o =&gt; o.Id);
      builder.Property(t =&gt; t.Username).HasColumnType(&quot;varchar&quot;).HasMaxLength(50);
      builder.Property(t =&gt; t.Email).HasColumnType(&quot;varchar&quot;);
  }
  </code></pre>
  <p>In the current example the Username and Email fields can contain null values in the DB (and they do).
  If I remove Username and Password from my class, then everything works. But whenever I have a nullable type I try to map, I get the DBnull exception.</p>
  <p>I'm very new to using EF with MySQL - so I figure there might be something I'm missing? :)</p>