简体   繁体   中英

Versioning Entity Framework entities

We are looking to implement a versioning scheme for our EF Code first data models. Take the following two models as an example:

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

Here, the idea is that a question has multiple alternatives. We want to link an alternative to a question's Id , but not its Version . Likewise, a question can get a reference to all alternatives which has a QuestionId equal to its Id .

How would this be modeled best? Feel free to change the models.

I looked at this for several minutes and maybe totally missed the boat, but this might be an option:

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

Theory here is that question and alternatives are all grouped together in one object. The question text is stored on the version, and you should be able to access the current version from the selected alternatives. You could change the Question object to either provide access to all QuestionText objects or just the alternatives.

Then again I could have totally missed the point of your Alternative .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM