对象表依赖于列ID - 代码优先

时间:2014-11-09 21:59:01

标签: c# database ef-code-first asp.net-mvc-5

我一直试图设置这个数据库,我不知道问题是什么。我搜索和搜索过,但没有任何帮助。这就是我想要做的事情:

Question.cs

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<Answer> Answers { get; set; }
        public virtual QuestionLinkToHashtag HashtagLinks { get; set; }

        public virtual Student Student { get; set; }

    }
}

QuestionLinkToHashtag.cs​​

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; }
    }
}

ProjectCruxContext.cs

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<Student> Students { get; set; }
        public DbSet<Question> Questions { get; set; }
        public DbSet<Answer> Answers { get; set; }
        public DbSet<QuestionLinkToHashtag> QuestionLinkToHashtags { get; set; }
        public DbSet<Hashtag> Hashtags { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Question>().HasRequired(x => x.HashtagLinks)
               .WithMany();

            modelBuilder.Entity<QuestionLinkToHashtag>().HasRequired(x => x.question)
               .WithMany()
               .HasForeignKey(x => x.questionId);

            base.OnModelCreating(modelBuilder);
        }
    }
}

基本上我们正在尝试实现一个非常类似于溢出的#标签系统。这是我在运行update-database命令时得到的错误:

对象&#39; PK_dbo.QuestionLinkToHashtags&#39;取决于列&quot; questionLinkToHashtagId&#39;。 ALTER TABLE DROP COLUMN questionLinkToHashtagId失败,因为一个或多个对象访问此列。

我不知道如何解决这个问题。我们首先使用代码,我能找到的所有帮助都在弄乱数据库本身,我不理解这一点。有谁可能解释问题是什么,为什么会发生,以及如何解决它?还是指出我正确的方向?谢谢!

最新的添加迁移

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 => 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 => 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 => 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 => c.Int(nullable: false));
            AddColumn("dbo.QuestionLinkToHashtags", "questionLinkToHashtagId", c => 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);
        }
    }
}

0 个答案:

没有答案