2 回答
TA贡献1820条经验 获得超9个赞
我不确定引入 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);
}
}
TA贡献1772条经验 获得超8个赞
首先,建议简单地将答案标记为“默认”的评论听起来不错,但试图帮助解决实际问题......
好的,我认为可以为您提供所需的编辑....
所以,我在这里使用的模型:
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 Question Question { get; set; }
//commented these out for the sake of this
//example
/*public IList<Formula> Formulas { get; set; }
public IList<Image> Images { get; set; }*/
}
public class DefaultAnswer
{
public int QuestionId { get; set; }
public int AnswerId { get; set; }
public Answer Answer { get; set; }
public Question Question { get; set; }
}
如此定义的关系:
//don't need a compound key for answers...
//easier to just let each answer be entirely unique.
modelBuilder.Entity<Answer>().HasKey(m => m.Id );
modelBuilder.Entity<Answer>().HasRequired(m => m.Question)
.WithMany(q => q.Answers).HasForeignKey(m => m.QuestionId);
modelBuilder.Entity<Question>()
.HasOptional(m => m.DefaultAnswer)
.WithOptionalDependent(m => m.Question);
//default answer is basically a mapping table between questions and answers.
//so it has a compound key. In fact the table should consist of ONLY these two key
//fields...
modelBuilder.Entity<DefaultAnswer>()
.HasKey(m => new { m.AnswerId, m.QuestionId });
这为我产生了这个:
CreateTable(
"dbo.DefaultAnswers",
c => new
{
AnswerId = c.Int(nullable: false),
QuestionId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.AnswerId, t.QuestionId })
.ForeignKey("dbo.Answers", t => t.AnswerId, cascadeDelete: true)
.Index(t => t.AnswerId);
唯一让我无法理解的是级联删除行为是如何设置的......有点担心删除“DefaultAnswer”记录会删除答案 - 当天太晚了,我的大脑无法解决这个问题我'我害怕!
- 2 回答
- 0 关注
- 165 浏览
添加回答
举报