c# - 实体框架 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,所以我DefaultAnswer
在Question模型中添加了一个导航属性。这是可选的,因为它可以是空白的。
所以,我试图建立这样的关系:
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_Id
在DefaultAnswers表中创建另一个名为的字段.....这是 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?
解决方案
我不确定引入 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);
}
}
推荐阅读
- .net-core - 如何不对我的通用主机使用的实体框架核心数据库的设置进行硬核?
- c++ - gcc 允许未初始化的 constexpr
- c# - 无法使用 ASPOSE Slides 设置表格单元格的边距
- java - 如何修复 ORACLE-ADF 中 FacesContext 的空指针异常
- java - 我的带有 FileWriter 的 Java 代码不起作用
- python - 什么是 python 的 View() 数据框命令等价物?
- php - AJAX 填充下拉列表中的未定义值
- java - 如何在Android中画布上的线条下绘制自定义阴影
- javascript - 并非所有变量都绑定 Node js 和 Oracle db
- json - 如何从 dataweave 2.0 脚本中删除引号?