title: EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax
body: <p>I am trying to create a foreign key relation ship with fluent syntax using EF code first approach.</p>
  
  <p>My entities are as follows,</p>
  
  <pre><code>public partial class Defect
  {
      public int DefectID { get; set; }
      public decimal ReleaseNo { get; set; }
      public int BuildNo { get; set; }
      public string Title { get; set; }
      public string Description { get; set; }
      public string StepsToReproduce { get; set; }
      public int ApplicationModuleID { get; set; }
      public int SeverityLevel { get; set; }
      public string LoggedBy { get; set; }
      public Nullable&lt;System.DateTime&gt; LoggedOn { get; set; }
      public string LastModifiedBy { get; set; }
      public Nullable&lt;System.DateTime&gt; LastModifiedOn { get; set; }
      public string AssignedTo { get; set; }
      public string Status { get; set; }
      public string ResolutionNote { get; set; }
      public Nullable&lt;System.DateTime&gt; ResolvedOn { get; set; }
      public int ProjectID { get; set; }
  
      public virtual SeverityIndex SeverityIndex { get; set; }
      public virtual User LoggedByUser { get; set; }
      public virtual User LastModifiedUser { get; set; }
      public virtual User AssignedToUser { get; set; }
      public virtual Project Project { get; set; }
  
  }
  public class DefectMap:EntityTypeConfiguration&lt;Defect&gt;
  {
      public DefectMap()
      {
          this.HasKey(d =&gt; d.DefectID);
          this.ToTable("Defect");
          this.Property(d =&gt; d.DefectID)
              .IsRequired()
              .HasColumnName("DefectID")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          this.Property(d =&gt; d.Description)
              .IsRequired()
              .IsUnicode()
              .IsVariableLength()
              .HasMaxLength(2000)
              .HasColumnName("Description")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
          this.Property(d =&gt; d.StepsToReproduce)
              .IsOptional()
              .IsUnicode()
              .IsVariableLength()
              .HasMaxLength(4000)
              .HasColumnName("StepsToReproduce")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
          this.Property(d =&gt; d.LastModifiedBy)
              .IsOptional()
              .IsUnicode()
              .IsVariableLength()
              .HasMaxLength(10)
              .HasColumnName("LastModifiedBy")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
          this.Property(d =&gt; d.AssignedTo)
              .IsOptional()
              .IsUnicode()
              .IsVariableLength()
              .HasMaxLength(10)
              .HasColumnName("AssignedTo")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
          this.Property(d =&gt; d.Status)
              .IsOptional()
              .IsUnicode()
              .IsVariableLength()
              .HasMaxLength(50)
              .HasColumnName("Status")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
          this.Property(d =&gt; d.ResolutionNote)
              .IsOptional()
              .IsUnicode()
              .IsVariableLength()
              .HasMaxLength(4000)
              .HasColumnName("ResolutionNote")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          this.HasRequired(p =&gt; p.Project).WithMany(p =&gt; p.DefectList).HasForeignKey(p =&gt; p.ProjectID);
          this.HasRequired(s =&gt; s.SeverityIndex).WithMany(s =&gt; s.DefectList).HasForeignKey(s =&gt; s.SeverityLevel).WillCascadeOnDelete();
          this.HasOptional(u =&gt; u.AssignedToUser).WithMany(u =&gt; u.AssignedToUserList).HasForeignKey(u =&gt; u.AssignedTo).WillCascadeOnDelete();
          this.HasOptional(u =&gt; u.LastModifiedUser).WithMany(u =&gt; u.ModifiedByUserList).HasForeignKey(u =&gt; u.LastModifiedBy);
          this.HasRequired(u =&gt; u.LoggedByUser).WithMany(u =&gt; u.LoggedByUserList).HasForeignKey(u =&gt; u.LoggedBy);
      }
  public partial class Project
  {
      public Project()
      {
          ApplicationModuleList = new List&lt;ApplicationModule&gt;();
          DefectList = new List&lt;Defect&gt;();
          UserList = new List&lt;User&gt;();
      }
  
      public int ID { get; set; }
      public string ProjectName { get; set; }
      public string ProjectManager { get; set; }
      public Nullable&lt;System.DateTime&gt; ProjectStartDate { get; set; }
      public Nullable&lt;System.DateTime&gt; ProjectEstimatedEndDate { get; set; }
      public Nullable&lt;System.DateTime&gt; ProjectActualEndDate { get; set; }
      public Nullable&lt;int&gt; ProjectBillingModel { get; set; }
      public Nullable&lt;decimal&gt; ProjectEstimatedBudget { get; set; }
      public Nullable&lt;decimal&gt; ProjectActualBudget { get; set; }
      public Nullable&lt;int&gt; ProjectPortfolio { get; set; }
      public Nullable&lt;decimal&gt; ProjectBillingRate { get; set; }
      public Nullable&lt;int&gt; ProjectEstimatedManHours { get; set; }
      public Nullable&lt;int&gt; ProjectActualManHours { get; set; }
      public Nullable&lt;int&gt; ProjectIsApproved { get; set; }
  
      public virtual ICollection&lt;ApplicationModule&gt; ApplicationModuleList { get; set; }
      public virtual ICollection&lt;Defect&gt; DefectList { get; set; }
      public virtual ICollection&lt;User&gt; UserList { get; set; }
      public virtual BillingModel BillingModel { get; set; }
      public virtual Portfolio Portfolio { get; set; }
      public virtual ApprovalStatus ApprovalStatus { get; set; }
  }
  public class ProjectMap:EntityTypeConfiguration&lt;Project&gt;
  {
      public ProjectMap()
      {
          this.HasKey(p =&gt; p.ID);
          this.ToTable("Projects");
          this.Property(p =&gt; p.ID)
              .HasColumnName("ID")
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
              .IsRequired();
          this.Property(p =&gt; p.ProjectName)
              .HasColumnName("ProjectName")
              .HasMaxLength(200)
              .IsRequired()
              .IsVariableLength()
              .IsUnicode()
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
  
          this.HasOptional(p =&gt; p.BillingModel).WithMany(p=&gt;p.Projects).HasForeignKey(p =&gt; p.ProjectBillingModel).WillCascadeOnDelete();
          this.HasOptional(p =&gt; p.Portfolio).WithMany(p=&gt;p.Projects).HasForeignKey(p =&gt; p.ProjectPortfolio).WillCascadeOnDelete();
          this.HasOptional(p =&gt; p.ApprovalStatus).WithMany(p=&gt;p.Projects).HasForeignKey(p =&gt; p.ProjectIsApproved).WillCascadeOnDelete();
      }
  }
  </code></pre>
  
  <p>I am trying code first approach for database creation using fluent API.</p>
  
  <p>However when I run the code I get error saying </p>
  
  <p>*Introducing FOREIGN KEY constraint 'FK_dbo.User_dbo.Projects_ProjectID' on table 'User' 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>
  
  <p>The same error appears for AssignedTo column.</p>
  
  <p>Here I am trying to implement logic where, A project can have many defects and a defect should have an associated Project ID (i.e one to many relationship between project and defect).</p>
  
  <p>Can anyone suggest what is wrong with the code and where should I rectify the code to get things working?</p>
  
  <p>Thanks in advance!!!</p>