title: Entity framework one to zero or one relationship without navigation property
body: <p>I hit an issue when trying to delete records due to FK constraints. I therefore went back to the drawing board and am trying to specify how the relationship should work.</p>
  
  <p>Here are my code first classes:</p>
  
  <pre><code>public class MemberDataSet
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public int? DeferredDataId { get; set; }
      [ForeignKey("DeferredDataId")]
      public virtual DeferredData DeferredData { get; set; }
  }
  
  public class DeferredData
  {
  
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      //other properties
  }
  </code></pre>
  
  <p>What I am looking to achieve is that the MemberDataSet has zero or one DeferredData. I can access DeferredData from MemberDataSet but DeferredData does not need a navigation property back to MemberDataSet. DeferredData should strictly require a MemberDataSet. In an ideal world deleting MemberDataSet will therefore delete DeferredData if assigned.</p>
  
  <p>What seemed to me to be what I wanted to specify is this:</p>
  
  <pre><code>modelBuilder.Entity&lt;MemberDataSet&gt;().HasOptional(d =&gt; d.DeferredData).WithRequired().WillCascadeOnDelete(true);
  </code></pre>
  
  <p>i.e. MemberDataSet has an option DeferredData but DeferredData has a required MemberDataSet and this relationship should cascade on delete.</p>
  
  <p>However, I then get an error:</p>
  
  <p>The ForeignKeyAttribute on property 'DeferredData' on type 'MemberDataSet' is not valid. The foreign key name 'DeferredDataId' was not found on the dependent type 'DeferredData'. The Name value should be a comma separated list of foreign key property names.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>After feeling happy with Sam's answer below I went ahead and changed a few other ForeignKey attributes. MemberDataSet has another property called SignedOffBy that is a userProfile. This previously looked like this:</p>
  
  <pre><code>public class MemberDataSet
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public int? DeferredDataId { get; set; }
      [ForeignKey("DeferredDataId")]
      public virtual DeferredData DeferredData { get; set; }
  
      public int? SignedOffById { get; set; }
      [ForeignKey("SignedOffId")]
      public virtual UserProfile SignedOffBy { get; set; }
  }
  </code></pre>
  
  <p>After discussion below on what ForeignKey attribute is actually doing I changed this to:</p>
  
  <pre><code>public class MemberDataSet
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public int? DeferredDataId { get; set; }
      [ForeignKey("Id")]
      public virtual DeferredData DeferredData { get; set; }
  
      public int? SignedOffById { get; set; }
      [ForeignKey("UserId")]
      public virtual UserProfile SignedOffBy { get; set; }
  }
  </code></pre>
  
  <p>However, I now get a very similar error message:</p>
  
  <pre><code>The ForeignKeyAttribute on property 'SignedOffBy' on type 'MemberDataSet' is not valid. The foreign key name 'UserId' was not found on the dependent type 'MemberDataSet'. The Name value should be a comma separated list of foreign key property names.
  </code></pre>
  
  <p>The difference here is that this relationship is Many to One i.e. 1 user can have several signedoff datasets. Is this what makes the difference? i.e. the UserProfile is now the principal object so the ForeignKey is on the MemberDataSet?</p>
  
  <p>Many thanks again for any and all help.</p>