gpt4 book ai didi

c# - 具有随机 ID 的虚拟存储库数据外键

转载 作者:行者123 更新时间:2023-11-30 17:40:11 25 4
gpt4 key购买 nike

我的项目中有两个虚拟存储库,一个用于问题,一个用于答案。这些问题是多项选择,所以他们可以有多个答案。我的问题模型:

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

}
}

您可能会注意到基类有一个 UniqueIdentifier 变量。此变量用于在在线数据库中创建唯一值(不能使用 id,因为用户在离线工作时可以创建相同的 id),答案应以 UniqueIdentifier 作为问题的外键。我应该如何获取/设置问题的答案,以便将它们加载到我的 View 中?

最佳答案

好的方法是使用Guid这将帮助您合并用户数据库,因为 Guid 具有 2^128 个唯一值。使用 Guid.NewGuid产生新的独特值(value)

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

关于c# - 具有随机 ID 的虚拟存储库数据外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com