我的项目中有两个虚拟存储库,一个用于问题,一个用于答案。问题是多重选择,因此他们可以有多个答案。我的问题模型:
public class Question : BaseClass
{
public Question() : base()
{
}
public int QuestionId { get; set; }
public string Value { get; set; }
public virtual List<Answer> Answers { get; set; }
}
答案属于问题
public class Answer : BaseClass
{
public Answer() : base()
{
}
public int AnswerId { get; set; }
public string Value { get; set; }
public int QuestionId { get; set; }
public virtual Question Question { get; set; }
}
它们都扩展了 BaseClass ,它有一些自定义字段。
public abstract class BaseClass
{
protected BaseClass()
{
UniqueIdentifier = RandomIdentifier(20);
}
public string UniqueIdentifier { get; set; }
private static string RandomIdentifier(int length)
{
//returns an unique identifier
}
}
我的 dummyQuestionRepository 如下所示:
public class DummyQuestionRepository : IQuestionRepository
{
private List<Question> _questions;
public DummyQuestionRepository()
{
_questions = new List<Question>();
_questions.Add(new Question { Value = "Favourit food?" });
_questions.Add(new Question { Value = "Who is the president?" });
_questions.Add(new Question { Value = "Favourit movie?" });
}
public List<Question> GetAll()
{
return _questions;
}
public void Create(Question q)
{
_questions.Add(q);
}
//removed the non relevant functions
}
我的 dummyAnswerRepository
class DummyAnswerRepository
{
private List<Answer> _answers;
public DummyAnswerRepository()
{
_answers = new List<Answer>();
_answers.Add(new Answer { Value = "pizza" });
_answers.Add(new Answer { Value = "fries" });
_answers.Add(new Answer { Value = "Bush" });
_answers.Add(new Answer { Value = "Obama" });
_answers.Add(new Answer { Value = "titanic" });
_answers.Add(new Answer { Value = "lion king" });
}
public List<Answer> GetAll()
{
return _answers;
}
public void Create(Answer a)
{
_answers.Add(a);
}
}
您可能会注意到baseclass有一个UniqueIdentifier变量。此变量用于在联机数据库中创建唯一值(因为用户可以在脱机工作时使用相同的ID,因此无法使用id),答案应将UniqueIdentifier作为外键。 我应该如何从问题中获取/设置答案,以便我可以在我的视图中加载它们?
答案 0 :(得分:1)
好方法是使用Guid来帮助您合并用户数据库,因为Guid有2 ^ 128个唯一值。使用Guid.NewGuid生成新的唯一值
public abstract class BaseClass
{
protected BaseClass()
{
UniqueIdentifier = Guid.NewGuid();
}
public Guid UniqueIdentifier { get; set; }
}
要添加外键,当答案和问题在一个位置创建然后添加到您的存储库时,您可以实现类似于Entity Framework Seed method的方法。只需删除从存储库构造函数创建新实体的代码,并使用如下代码:
public class DataBaseInitializer
{
public void Seed(IQuestionRepository questionRepository, DummyAnswerRepository answerRepository)
{
var q1 = new Question { Value = "Favourit food?" };
var q2 = new Question { Value = "Who is the president?" });
var q3 = new Question { Value = "Favourit movie?" });
questionRepository.Create(q1);
questionRepository.Create(q2);
questionRepository.Create(q3);
answerRepository.Create(new Answer { Value = "pizza", Question = q1 });
answerRepository.Create(new Answer { Value = "fries", Question = q1 });
answerRepository.Create(new Answer { Value = "Bush", Question = q2 });
answerRepository.Create(new Answer { Value = "Obama", Question = q2 });
answerRepository.Create(new Answer { Value = "titanic", Question = q3 });
answerRepository.Create(new Answer { Value = "lion king", Question = q3 });
}
}