版本化实体框架实体

时间:2013-04-10 12:38:13

标签: c# entity-framework ef-code-first entity-framework-5

我们正在寻求为EF Code第一数据模型实现版本控制方案。以下面两个模型为例:

public class Question
{
    public int Id { get; set; }
    public int Version { get; set; }
    public string Text { get; set; }

    public ICollection<Alternative> Alternatives { get; set; }
}

public class Alternative
{
    public int Id { get; set; }
    public int Version { get; set; }
    public string Text { get; set; }
    public bool IsCorrect { get; set; }

    public int QuestionId { get; set; }
    public virtual Question Question { get; set; }
}

这里的想法是,一个问题有多种选择。我们希望将问题的Id替代,而不是Version。同样,问题可以引用QuestionId等于Id的所有替代方案。

如何最好地建模?随意更改模型。

1 个答案:

答案 0 :(得分:2)

我看了几分钟,也许完全错过了船,但这可能是一个选择:

public class Question
{
    public int QuestionId { get; set; }
    public QuestionText Primary { get; set; }
    public ICollection<QuestionText> Alternatives { get; set; }
}

public class QuestionText
{
    public int QuestionTextId { get; set; }
    public ICollection<QuestionTextVersion> Versions { get; set; }
    public QuestionTextVersion CurrentVersion { get; set; }
}

public class QuestionTextVersion
{
    public int QuestionTextVersionId { get; set; }
    public int Version { get; set; }
    public string Text { get; set; }
}

这里的理论是问题和替代方案都集中在一个对象中。问题文本存储在版本中,您应该能够从所选备选方案中访问当前版本。您可以更改Question对象,以提供对所有QuestionText个对象的访问权限,或仅提供替代方案。

然后我再次完全错过了Alternative