title: Entity Framework: how to solve "FOREIGN KEY constraint may cause cycles or multiple cascade paths"?
body: <p>there are lots of questions about this problem, but I couldn't solve my case.
  can some one please take a look at this:</p>
  
  <p>I have an <code>Office</code> table which has one-many relationship with <code>Doctor</code> and <code>Secretary</code> tables. Both of the last tables, are derived from <code>Employee</code> table which has a shared-primary-key relationship with predefined <code>Users</code> table created by <code>sqlmembershipprovider</code>. It seems there is a many-many relationship between <code>Users</code> table and <code>Roles</code> table which I haven't any hand in it.</p>
  
  <p>My problem was in creating a (zero,one) - (one) relationship between my <code>Employee</code> table and that <code>Users</code> table which I ended with a shared primary key relationship between them and the error raised, then. (Is there a better solution for that problem?)</p>
  
  <p><strong>here is the error:</strong></p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint
    'FK_dbo.aspnet_UsersInRoles_dbo.aspnet_Users_UserId' on table
    'aspnet_UsersInRoles' may cause cycles or multiple cascade paths.
    Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other
    FOREIGN KEY constraints. Could not create constraint. See previous
    errors.</p>
  </blockquote>
  
  <p><strong>here are my codes and membership codes after reverse engineering:</strong></p>
  
  <pre><code>public class Office
  {
      public Office()
      {
          this.Doctors = new HashSet&lt;Doctor&gt;();
          this.Secretaries = new HashSet&lt;Secretary&gt;();
      }
  
      [Key]
      public System.Guid OfficeId { get; set; }
      public virtual ICollection&lt;Doctor&gt; Doctors { get; set; }
      public virtual ICollection&lt;Secretary&gt; Secretaries { get; set; }
  }
  
  public class Employee
  {
      [Key, ForeignKey("User")]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public System.Guid Id { get; set; }
      public string Name { get; set; }
  
      [ForeignKey("Office")]
      public System.Guid OfficeId { get; set; }
  
      // shared primary key 
      public virtual aspnet_Users User { get; set; }
  
      public virtual Office Office { get; set; }
  }
  
  public class Doctor :Employee
  {
      public Doctor()
      {
          this.Expertises = new HashSet&lt;Expertise&gt;();
      }
      //the rest..    
      public virtual ICollection&lt;Expertise&gt; Expertises { get; set; }
  }
  
  public class Secretary : Employee
  {
      // blah blah
  }
  
  public class aspnet_Users
  {
      public aspnet_Users()
      {
          this.aspnet_Roles = new List&lt;aspnet_Roles&gt;();
      }
  
      public System.Guid ApplicationId { get; set; }
      public System.Guid UserId { get; set; }
      //the rest..
      public virtual aspnet_Applications aspnet_Applications { get; set; }
      public virtual ICollection&lt;aspnet_Roles&gt; aspnet_Roles { get; set; }
  }
  
  public class aspnet_Roles
  {
      public aspnet_Roles()
      {
          this.aspnet_Users = new List&lt;aspnet_Users&gt;();
      }
  
      public System.Guid ApplicationId { get; set; }
      public System.Guid RoleId { get; set; }
      //the rest..
      public virtual aspnet_Applications aspnet_Applications { get; set; }
      public virtual ICollection&lt;aspnet_Users&gt; aspnet_Users { get; set; }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> and the relationships go deeper, there is a many-one relationship between <code>Users</code> table and <code>Applications</code> table, also between <code>Roles</code> and <code>Applications</code> too.</p>