在code-第一个SQL Server数据库中存储对象的列表 [英] Storing list of objects in a SQL Server database with code-first

查看:135
本文介绍了在code-第一个SQL Server数据库中存储对象的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例

让我们说的选择题答案的数量而变化。一个问题只能有两种选择,而另一个可以有10个选项。

Let's say the number of answers of multiple choice questions varies. One question can have only 2 choices while another can have 10 choices.

我如何保存我的SQL Server数据库中的这些选择题?是下面的模型可行?

How can I save these multiple choice questions in my SQL Server database? Is the following model viable?

public class MultipleChoiceQuestion
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    public string Question { get; set; }

    // Suppose I can have 2-10 answers
    [Required]
    public List<string> Answers { get; set; }
}

问题


  • 我们可以存储与各种长度的对象列表中,在启用ASP.NET code-第一次迁移SQL Server数据库?

  • 如果它是不适用的,什么是解决这些问题的最佳解决方案?

推荐答案

您可以创建类型PossibleAnswer将重新present针对任何特定选择题每个答案的另一个对象。然后,你会修改你的MultipleChoiceQuestion对象的答案属性为列表类型。 PossibleAnswer可以定义为如下:

You could create another object of type PossibleAnswer that would represent each individual answer for any particular multiple choice question. Then you would modify your Answers property in your MultipleChoiceQuestion object to be of type List. PossibleAnswer could be defined as follows:

 public class PossibleAnswer {
      public Guid Id {get;set;}
      public Guid MultipleChoiceQuestionId {get;set;}
      public string Answer {get;set;}
 }

这篇关于在code-第一个SQL Server数据库中存储对象的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆