title: Cascade Delete Rule in EF 4.1 Code First when using Shared Primary Key Association
body: <p>I implemented a bidirectional 1:1 relationship based on this answer:</p>
  
  <p><a href="https://stackoverflow.com/questions/5388077/primary-foreign-key-in-entity-framework/5388658#5388658">Primary /Foreign Key in Entity Framework</a></p>
  
  <p>I define the bidirectional relation this way:</p>
  
  <pre><code>public class Student
  {   
      public virtual int StudentId { get; set; }
      public virtual Anamnesis Anamnesis { get; set; }
  
      . . .
  }
  
  public class Anamnesis
  {
      [Key, ForeignKey("Student")]
      public int AnamnesisId { get; set; }
  
      public virtual Student Student { get; set; }
  
      . . .
  }
  </code></pre>
  
  <p>where, Student is the principal entity and Anamnesis it the entity that shares the PK.</p>
  
  <p>Now I'd like that the relationship created had a Delete Rule = CASCADE. Actually, the relationship that is being created has Delete Rule = NO ACTION as seen in the following picture:</p>
  
  <p><img src="https://i.stack.imgur.com/Y9juR.jpg" alt="enter image description here"></p>
  
  <p>If I manually delete this relation inside the Table Properties window and add other relation with Delete Rule = CASCADE, the code works as I expect allowing me to delete a Student and it's shared Anamnesis that has the same ID.</p>
  
  <p>So, here goes my question:</p>
  
  <p>Is there a way of using Data Annotation (not Fluent API) in my class so that I get a Relation with CASCADE delete rule? I'd prefer using Data Annotation but if it's not possible, I'd be happy with some Fluent API code that makes this work.</p>
  
  <p><strong>NOTE</strong></p>
  
  <p>I have tried the Fluent API code that is shown in this <a href="http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx" rel="nofollow noreferrer">post</a>. It doesn't work in my case where I have bidirectional properties.</p>