【问题标题】:How to use enum and json to insert data in database?如何使用 enum 和 json 在数据库中插入数据?
【发布时间】:2021-04-28 09:59:54
【问题描述】:

我正在尝试创建一个问题表,其中包含描述性和选择以及是否问题,并且我想将所有问题插入一个表中。 当我想创建选择问题时,我想使用 enum 和 json 将选择插入表中,就像 YesNo 问题一样。我也想选择问题表的正确答案。我怎样才能做到这一点? 这是我的问题课:

 public class Question
{
    [Key]
    public int Id { get; set; }//1
    public int CourseId { get; set; }
    public string QuestionTitle { get; set; }
    public decimal Grade { get; set; }
    public DateTime TimeDuration { get; set; }
    public DateTime StartTime { get; set; }
    [EnumDataType(typeof(Choice))]
    public string Choice { get; set; }
    public string CorrectChoice { get; set; }
    [EnumDataType(typeof(YesNo))]
    public bool? YesNo { get; set; }
    public string Descriptive { get; set; }

    #region relations
    public virtual IdentityUser IdentityUser { get; set; }
    public Course course { get; set; }
    #endregion
}
public enum Choice
{
    Choice1, Choice2, Choice3, Choice4
}
public enum YesNo
{
    Yes, No
}

当我尝试为问题创建单独的表(描述表、YesNo 表、选择表)时,我的主管说这是不对的,您应该按照上述方式进行操作。

【问题讨论】:

  • 您能解释一下您当前尝试的问题是什么吗?
  • 我不知道如何将问题选项插入表格。

标签: c# asp.net json asp.net-core enums


【解决方案1】:

我认为您正在寻找一个额外的表格来存储您的选择,因为您需要每个问题的选择列表,对吧?

public class QuestionChoice
{
    [Key]
    public int Id { get; set; }
    public string Description { get; set; }
    public bool IsTheCorrectAnswer { get; set; }

    #region relations
    // This should point to the question the choice belongs to
    public Question Question { get; set; }
    #endregion
}

然后使用导航属性更新您的 Question 类到选项列表:

public class Question
{
    [Key]
    public int Id { get; set; }
    public int CourseId { get; set; }
    public string QuestionTitle { get; set; }
    public decimal Grade { get; set; }
    public DateTime TimeDuration { get; set; }
    public DateTime StartTime { get; set; }
    [EnumDataType(typeof(YesNo))]
    public bool? YesNo { get; set; }
    public string Descriptive { get; set; }

    #region relations
    public virtual IdentityUser IdentityUser { get; set; }
    public Course course { get; set; }
    
    // New property instead of 'Choice' and 'CorrectChoice'
    public List<QuestionChoice> Choices { get; set; }
    #endregion
}

插入和检索带有选项的问题:

var question = new Question
{
    // Set all properties...

    Descriptive = "What color is a Marigold flower?",
    Choices = new List<QuestionChoice>
    {
        new QuestionChoice
        {
            Description = "It's red!",
            IsTheCorrectAnswer = false
        },
        new QuestionChoice
        {
            Description = "It's yellow!",
            IsTheCorrectAnswer = true
        }
    }
};

// Insert into the database
_dbContext.Questions.Add(question);
_dbContext.SaveChanges();

// Read from the database
var question = _dbContext.Questions.FirstOrDefault(q => q.Id == 1); // Whatever ID it has
var choices = question.Choices;
// ...

【讨论】:

  • 实际上一开始我尝试这样做,但我的主管说我应该使用 enum 和 json 并且对于选择我应该考虑一个字段。不知道有没有可能。
  • 嗯,“使用 enum 和 json”非常含糊,所以我真的不知道那是什么意思。如果我的例子不是你的解决方案,我认为你需要和你的主管多谈谈,因为那时你还不清楚你需要什么。
  • 他坚持只使用一个表并将一个对象(选择)转换为 json 然后插入到数据库中。我会尝试这个解决方案并向他展示。谢谢
猜你喜欢
  • 2012-12-05
  • 2019-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
相关资源
最近更新 更多