title: Nullability for strings vs other data types in columns created by Entity Framework Code First
body: <p>When defining a string in a class using Code First, the default for the columns in the generated database allows nulls.</p>
  
  <pre><code>    public string MyString { get; set; }
  </code></pre>
  
  <p>creates this column: </p>
  
  <p><strong>MyString (nvarchar(max), null)</strong></p>
  
  <p>I can change this to "not null" by using the <code>[Required]</code> attribute or by using the .IsRequired() method in the Fluent API.</p>
  
  <p>The default for other data types does not allow nulls.</p>
  
  <pre><code>    public int MyInt { get; set; }
      public DateTime MyDateTime { get; set; }
      public float MyFloat { get; set; }
      public decimal MyDecimal { get; set; }
      public bool MyBool { get; set; }
  </code></pre>
  
  <p>creates these columns:</p>
  
  <p><strong>MyInt (int, not null)</strong></p>
  
  <p><strong>MyDateTime (datetime, not null)</strong></p>
  
  <p><strong>MyFloat (real, not null)</strong></p>
  
  <p><strong>MyDecimal (decimal(18,2), not null)</strong></p>
  
  <p><strong>MyBool (bit, not null)</strong></p>
  
  <p>I can change these to allow nulls by using the .IsOptional() method in the Fluent API. Is there an attribute that will do the same thing? </p>
  
  <p>I can also change the data types in the class definition to allow nulls (int?, DateTime?, etc.) which results in columns that allow nulls in the database.</p>
  
  <p>What is the rationale for allowing nulls by default for strings but not for other data types?</p>