title: EF: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
body: <p>I am trying to use the <code>update-database</code> command on a new table that I am trying to create and I am provided with the following message: </p>
  
  <blockquote>
    <p>The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
    "FK_dbo.ActivityLog_dbo.Severity_SeverityLevel". The conflict occurred
    in database "APGameLogs", table "dbo.Severity", column 'Level'.</p>
  </blockquote>
  
  <p>Am I doing something wrong? Is this because my foreign key <code>SeverityLevel</code> in the <code>ActivityLog</code> table is a different name than the primary key <code>Level</code> in the <code>Severity</code> table?</p>
  
  <p><strong>My entities are as follows:</strong></p>
  
  <pre><code>public class ActivityLog
  {
      public int ActivityLogId { get; set; }
  
      [Required]
      public int ActionLogId { get; set; }
  
      [Required]
      public int ActivityId { get; set; }
  
      [Range(0, 7)]
      public int SeverityLevel { get; set; }
  
      [Required]
      public DateTime ActivityDate { get; set; }
  
      // Navigation properties
      public ActionLog ActionLog { get; set; }
      public Activity Activity { get; set; }
  
      [ForeignKey("SeverityLevel")]
      public Severity Severity { get; set; }
  
  }
  
  public class Severity
  {
      public Severity()
      {
          this.ActivityLogs = new HashSet&lt;ActivityLog&gt;();
      }
  
      [Key]
      [Range(0,7)]
      public int Level { get; set; }
  
      [MaxLength(50)]
      [Required]
      public string Name { get; set; }
  
      [MaxLength(256)]
      public string Description { get; set; }
  
      // Navigation properties
      public virtual ICollection&lt;ActivityLog&gt; ActivityLogs { get; set; }
  
  }
  </code></pre>
  
  <p><strong>My migration is as follows</strong></p>
  
  <pre><code>public partial class Severity : DbMigration
      {
          public override void Up()
          {
              CreateTable(
                  "dbo.Severity",
                  c =&gt; new
                      {
                          Level = c.Int(nullable: false, identity: true),
                          Name = c.String(nullable: false, maxLength: 50),
                          Description = c.String(maxLength: 256),
                      })
                  .PrimaryKey(t =&gt; t.Level)
                  .Index(t =&gt; t.Name, unique: true, name: "UK_Severity_Name");
  
              AddColumn("dbo.ActivityLog", "SeverityLevel", c =&gt; c.Int(nullable: false));
              CreateIndex("dbo.ActivityLog", "SeverityLevel");
              AddForeignKey("dbo.ActivityLog", "SeverityLevel", "dbo.Severity", "Level");
          }
  
          public override void Down()
          {
              DropForeignKey("dbo.ActivityLog", "SeverityLevel", "dbo.Severity");
              DropIndex("dbo.Severity", "UK_Severity_Name");
              DropIndex("dbo.ActivityLog", new[] { "SeverityLevel" });
              DropColumn("dbo.ActivityLog", "SeverityLevel");
              DropTable("dbo.Severity");
          }
      }
  </code></pre>
  
  <p>** This is what the package manager was doing when it failed:**</p>
  
  <pre><code>Applying explicit migration: 201510161347096_Severity.
  
  CREATE TABLE [dbo].[Severity] (
      [Level] [int] NOT NULL IDENTITY,
      [Name] [nvarchar](50) NOT NULL,
      [Description] [nvarchar](256),
      CONSTRAINT [PK_dbo.Severity] PRIMARY KEY ([Level])
  )
  CREATE UNIQUE INDEX [UK_Severity_Name] ON [dbo].[Severity]([Name])
  ALTER TABLE [dbo].[ActivityLog] ADD [SeverityLevel] [int] NOT NULL DEFAULT 0
  CREATE INDEX [IX_SeverityLevel] ON [dbo].[ActivityLog]([SeverityLevel])
  ALTER TABLE [dbo].[ActivityLog] ADD CONSTRAINT [FK_dbo.ActivityLog_dbo.Severity_SeverityLevel] FOREIGN KEY ([SeverityLevel]) REFERENCES [dbo].[Severity] ([Level])
  </code></pre>