首页 > 解决方案 > 实体框架 6 映射外键的问题

问题描述

我有 3 个实体:问题

public enum QuestionType
{
    Scenario,
    Step
}

public class Question
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    [MaxLength(255)] public string FrameText { get; set; }
    public int Order { get; set; }
    public QuestionType Type { get; set; }

    public DefaultAnswer DefaultAnswer { get; set; }
    public IList<Answer> Answers { get; set; }
}

回答

public class Answer
{
    public int Id { get; set; }
    public int QuestionId { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    public int Order { get; set; }

    public Scenario Scenario { get; set; }
    public IList<Formula> Formulas { get; set; }
    public IList<Image> Images { get; set; }
}

最后,DefaultAnswer

public class DefaultAnswer
{
    public int QuestionId { get; set; }
    public int AnswerId { get; set; }

    public Answer Answer { get; set; }
    public Question Question { get; set; }
}

问题答案之间的关系是显而易见的。我想为一个问题设置一个DefaultAnswer,所以我DefaultAnswerQuestion模型中添加了一个导航属性。这是可选的,因为它可以是空白的。

所以,我试图建立这样的关系:

modelBuilder.Entity<DefaultAnswer>().HasKey(m => new { m.QuestionId, m.AnswerId });
modelBuilder.Entity<Question>().HasOptional(m => m.DefaultAnswer).WithRequired(m => m.Question);
modelBuilder.Entity<DefaultAnswer>().HasRequired(m => m.Answer).WithMany().HasForeignKey(m => m.AnswerId);

但这不起作用,它会Question_IdDefaultAnswers表中创建另一个名为的字段.....这是 EF 生成的:

public override void Up()
{
    CreateTable(
        "dbo.DefaultAnswers",
        c => new
            {
                QuestionId = c.Int(nullable: false),
                AnswerId = c.Int(nullable: false),
                Question_Id = c.Int(nullable: false),
            })
        .PrimaryKey(t => new { t.QuestionId, t.AnswerId })
        .ForeignKey("dbo.Answers", t => t.AnswerId, cascadeDelete: true)
        .ForeignKey("dbo.Questions", t => t.Question_Id)
        .Index(t => t.AnswerId)
        .Index(t => t.Question_Id);

}

这不是我想要的。我实际上想要这个:

public override void Up()
{
    CreateTable(
        "dbo.DefaultAnswers",
        c => new
            {
                QuestionId = c.Int(nullable: false),
                AnswerId = c.Int(nullable: false),
            })
        .PrimaryKey(t => new { t.QuestionId, t.AnswerId })
        .ForeignKey("dbo.Answers", t => t.AnswerId, cascadeDelete: true)
        .ForeignKey("dbo.Questions", t => t.QuestionId)
        .Index(t => t.AnswerId)
        .Index(t => t.QuestionId);

}

有谁知道我如何在保持导航属性的同时创建这种关系Question

标签: c#entity-framework

解决方案


我不确定引入 DefaultAnswer 实体的意图是什么。但是这种关系可以在没有它的情况下建模。例如:

public enum QuestionType
{
    Scenario,
    Step
}

public class Question
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    [MaxLength(255)] public string FrameText { get; set; }
    public int Order { get; set; }
    public QuestionType Type { get; set; }

    public int? DefaultAnswerId { get; set; }

    [ForeignKey("Id,DefaultAnswerId")]
    public Answer DefaultAnswer { get; set; }
    public IList<Answer> Answers { get; set; }
}
public class Answer
{
    public int Id { get; set; }
    public int QuestionId { get; set; }
    [Required] [MaxLength(255)] public string Text { get; set; }
    public int Order { get; set; }

    public virtual Question Question { get; set; }

    //public Scenario Scenario { get; set; }
    //public IList<Formula> Formulas { get; set; }
    //public IList<Image> Images { get; set; }
}
class Db: DbContext
{
    public DbSet<Question> Question { get; set; }
    public DbSet<Answer> Answer { get; set; }



    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Answer>().HasKey(m => new { m.QuestionId, m.Id });
        modelBuilder.Entity<Answer>().HasRequired(m => m.Question).WithMany(q => q.Answers).HasForeignKey( m => m.QuestionId);
        modelBuilder.Entity<Question>().HasOptional(m => m.DefaultAnswer);


    }


}

推荐阅读