title: Entity framework code first convert between class boolean and column integer
body: <p>I am using <code>Entity Framework 5 code first</code>. My table has a column called <code>Active</code> and its datatype is of type <code>int</code>.  The values that are stored in Active are <code>0</code>, <code>1</code> and <code>null</code>.</p>
  
  <p>I have a class that I need to map to this table.</p>
  
  <pre><code>public class CommandExecutionServer : IEntity
  {
       public int Id { get; set; }
  
       public bool? IsActive { get; set; }
  }
  </code></pre>
  
  <p>Here is my configuration file.  I am trying to map my boolean property in my class to the integer field in the database.</p>
  
  <pre><code>class CommandExecutionServerConfiguration : EntityTypeConfiguration&lt;CommandExecutionServer&gt;
  {
       internal CommandExecutionServerConfiguration()
       {
            this.ToTable("tblCommandExecutionServers");
            this.Property(x =&gt; x.IsActive).HasColumnName("Active").HasColumnType("bit");
       }
  }
  </code></pre>
  
  <p>This is not working well.  The error that I am getting is:</p>
  
  <pre><code>The 'IsActive' property on 'CommandExecutionServer' could not be set to a 'Int32' value. You must set this property to a non-null value of type 'Boolean'
  </code></pre>
  
  <p>I tried adding <code>.HasColumnType("bit")</code> and thought that it might take of my problem.  How do I do this?  Ideally I would like 0 to be false, 1 to true, null to be null, and any other number to false.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>If I change the above to:</p>
  
  <pre><code>this.Property(x =&gt; x.IsActive).HasColumnName("Active").HasColumnType("int");
  </code></pre>
  
  <p>...then I get the following error:</p>
  
  <pre><code>Member Mapping specified is not valid. The type 'Edm.Boolean[Nullable=True,DefaultValue=]' of member 'IsActive' in type 'MyProject.Infrastructure.EntityFramework.CommandExecutionServer' is not compatible with 'SqlServer.int[Nullable=True,DefaultValue=]' of member 'Active' in type 'CodeFirstDatabaseSchema.CommandExecutionServer'.
  </code></pre>