title: Unique constraint on nullable in System.Data.DataColumn
body: <p>In my database I have a table like this</p>
  
  <pre><code>table foo
  int pk
  int someFK NULL
  </code></pre>
  
  <p>with foreign key constraint on someFK and unique constraint on someFK. 
  This means in the MySQL database that I have that unless I specify NULL in someFK, there has to be a row in the corresponding table of course. However, I can also have several rows with NULL in someFK even if the unique constraint is on.</p>
  
  <p>In my code, I use System.Data namespace and do like this:</p>
  
  <pre><code>DataTable table = new DataTable("Foo");
  
  DataColumn col = null;
  
  DataColumn[] primaryKey = new DataColumn[1];
  
  col = table.Columns.Add(FooPropertyName, typeof(int));
  col.Unique = true;
  col.AutoIncrement = true;
  primaryKey[0] = col;
  table.PrimaryKey = primaryKey;
  
  col = table.Columns.Add(SomeFkPropertyName, typeof(int));
  col.Unique = true;
  col.AutoIncrement = false;
  </code></pre>
  
  <p>However, if I add two DataRows to my DataTable, and those two have different primary keys but both have DBNull on the someFK column, I get an error message 
  Exception Type:        System.Data.ConstraintException
  Exception Message:     Column 'somefk' is constrained to be unique.  Value '' is already present.</p>
  
  <p>This is not what I expect, so I was wondering if someone knows how to get around this (without removing the unique property)</p>