title: Object Table is dependent on Column ID - Code First
body: <p>I've been trying to get this database set up and I have not a clue what the problem is. I've searched and searched but nothing is helping. Here is what I'm trying to do:</p>
  
  <p>Question.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Linq;
  using System.Web;
  
  namespace ProjectCrux.Models
  {
      public class Question
      {
          public int questionId { get; set; }
  
          [Required]
          [Display(Name = "Question")]
          public string question { get; set; }
  
          public int studentID { get; set; }  // FK
          public int hashtagLinksID { get; set; }
  
          public virtual ICollection&lt;Answer&gt; Answers { get; set; }
          public virtual QuestionLinkToHashtag HashtagLinks { get; set; }
  
          public virtual Student Student { get; set; }
  
      }
  }
  </code></pre>
  
  <p>QuestionLinkToHashtag.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Linq;
  using System.Web;
  
  namespace ProjectCrux.Models
  {
      public class QuestionLinkToHashtag
      {
          public int questionLinkToHashtagId { get; set; }
  
          public int questionId { get; set; } //FK to question
  
          public int hashtagsId { get; set; } //FK to hashtag
  
  
          public virtual Question question { get; set; }
  
          public virtual Hashtag hashtags { get; set; }
      }
  }
  </code></pre>
  
  <p>ProjectCruxContext.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  
  namespace ProjectCrux.Models
  {
      public class ProjectCruxContext : DbContext
      {
          public ProjectCruxContext()
              : base("name=DefaultConnection")
          {
  
          }
          public DbSet&lt;Student&gt; Students { get; set; }
          public DbSet&lt;Question&gt; Questions { get; set; }
          public DbSet&lt;Answer&gt; Answers { get; set; }
          public DbSet&lt;QuestionLinkToHashtag&gt; QuestionLinkToHashtags { get; set; }
          public DbSet&lt;Hashtag&gt; Hashtags { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Question&gt;().HasRequired(x =&gt; x.HashtagLinks)
                 .WithMany();
  
              modelBuilder.Entity&lt;QuestionLinkToHashtag&gt;().HasRequired(x =&gt; x.question)
                 .WithMany()
                 .HasForeignKey(x =&gt; x.questionId);
  
              base.OnModelCreating(modelBuilder);
          }
      }
  }
  </code></pre>
  
  <p>basically we're trying to implement a hashtag system very similar to overflow's. this is the error I'm getting when run the update-database command:</p>
  
  <p>The object 'PK_dbo.QuestionLinkToHashtags' is dependent on column 'questionLinkToHashtagId'.
  ALTER TABLE DROP COLUMN questionLinkToHashtagId failed because one or more objects access this column.</p>
  
  <p>I have no idea how to work around this. We are using code first and all the help I've been able to find are messing with the database itself and I don't understand that one bit. Could anyone possibly explain what the problem is, why its happening, and how to fix it? Or point me in the right direction? Thanks!</p>
  
  <p>Latest Add-Migration</p>
  
  <pre><code>namespace ProjectCrux.Migrations
  {
      using System;
      using System.Data.Entity.Migrations;
  
      public partial class QuestinHashtagUpdate1 : DbMigration
      {
          public override void Up()
          {
              DropForeignKey("dbo.QuestionLinkToHashtags", "questionId", "dbo.Questions");
              DropForeignKey("dbo.Hashtags", "QuestionLinkToHashtag_questionLinkToHashtagId", "dbo.QuestionLinkToHashtags");
              DropIndex("dbo.QuestionLinkToHashtags", new[] { "questionId" });
              DropIndex("dbo.Hashtags", new[] { "QuestionLinkToHashtag_questionLinkToHashtagId" });
              DropColumn("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId");
              DropColumn("dbo.QuestionLinkToHashtags", "hashtagId");
              RenameColumn(table: "dbo.QuestionLinkToHashtags", name: "questionId", newName: "questionLinkToHashtagId");
              RenameColumn(table: "dbo.QuestionLinkToHashtags", name: "QuestionLinkToHashtag_questionLinkToHashtagId", newName: "hashtagId");
              DropPrimaryKey("dbo.QuestionLinkToHashtags");
              AlterColumn("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId", c =&gt; c.Int(nullable: false));
              AddPrimaryKey("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId");
              CreateIndex("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId");
              CreateIndex("dbo.QuestionLinkToHashtags", "hashtagId");
              AddForeignKey("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId", "dbo.Questions", "questionId");
              AddForeignKey("dbo.QuestionLinkToHashtags", "hashtagId", "dbo.Hashtags", "hashtagId", cascadeDelete: true);
              DropColumn("dbo.Hashtags", "QuestionLinkToHashtag_questionLinkToHashtagId");
          }
  
          public override void Down()
          {
              AddColumn("dbo.Hashtags", "QuestionLinkToHashtag_questionLinkToHashtagId", c =&gt; c.Int());
              DropForeignKey("dbo.QuestionLinkToHashtags", "hashtagId", "dbo.Hashtags");
              DropForeignKey("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId", "dbo.Questions");
              DropIndex("dbo.QuestionLinkToHashtags", new[] { "hashtagId" });
              DropIndex("dbo.QuestionLinkToHashtags", new[] { "questionLinkToHashtagId" });
              DropPrimaryKey("dbo.QuestionLinkToHashtags");
              AlterColumn("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId", c =&gt; c.Int(nullable: false, identity: true));
              AddPrimaryKey("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId");
              RenameColumn(table: "dbo.QuestionLinkToHashtags", name: "hashtagId", newName: "QuestionLinkToHashtag_questionLinkToHashtagId");
              RenameColumn(table: "dbo.QuestionLinkToHashtags", name: "questionLinkToHashtagId", newName: "questionId");
              AddColumn("dbo.QuestionLinkToHashtags", "hashtagId", c =&gt; c.Int(nullable: false));
              AddColumn("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId", c =&gt; c.Int(nullable: false, identity: true));
              CreateIndex("dbo.Hashtags", "QuestionLinkToHashtag_questionLinkToHashtagId");
              CreateIndex("dbo.QuestionLinkToHashtags", "questionId");
              AddForeignKey("dbo.Hashtags", "QuestionLinkToHashtag_questionLinkToHashtagId", "dbo.QuestionLinkToHashtags", "questionLinkToHashtagId");
              AddForeignKey("dbo.QuestionLinkToHashtags", "questionId", "dbo.Questions", "questionId", cascadeDelete: true);
          }
      }
  }
  </code></pre>