title: EF Code First One to (One or Zero) with string as primary key
body: <p>I keep getting errors when I try to link up these two classes for the data structure. One has a string as a primary key and the other has a long. I've tried data annotations and other various things.</p>
  
  <p>Fluent API or Data Annotations are acceptable.</p>
  
  <p>Here's a sample of the one class:</p>
  
  <pre><code>public class X
  {
      [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
      public long Id { get; set; }
  
      public string Token { get; set; }
      public virtual Y Y { get; set; }
  }
  </code></pre>
  
  <p>Here's the other class:</p>
  
  <pre><code>public class Y
  {
      [Key, StringLength(64), DatabaseGenerated(DatabaseGeneratedOption.None)]
      public string Token { get; set; }
  
      public long XId { get; set; }
      public virtual X X{ get; set; }
  }
  </code></pre>
  
  <p>And here is where I'm trying to define fkeys in OnModelCreating():</p>
  
  <pre><code>modelBuilder.Entity&lt;X&gt;()
              .HasOptional(u =&gt; u.Y)
              .WithRequired(u =&gt; u.X)
              .Map(m =&gt; m.MapKey("Token"));
  
  modelBuilder.Entity&lt;Y&gt;()
              .HasRequired(u =&gt; u.X)
              .WithOptional(u =&gt; u.Y)
              .Map(m =&gt; m.MapKey("XId"));
  </code></pre>