将基本类型的简单列表添加到数据库表的代码优先语法是什么?

What is the codefirst syntax to add a simple list of an elementary type to a database table?

希望我简化了所有内容以专注于我的问题。我正在定义 class 以使用代码优先 EF 创建数据库。当我执行以下操作时:

Public Class question
  Public Property QuestionID As Integer
  Public Property Answers As String
End Class

我在迁移文件中得到了这个:

CreateTable(
  "dbo.questions",
  Function(c) New With
    {
      .QuestionID = c.Int(nullable:=False, identity:=True),
      .Answers = c.String()
    }) _
  .PrimaryKey(Function(t) t.QuestionID)

到目前为止,很好。现在,当我尝试将第二个属性更改为字符串列表时,如下所示:

Public Class question
  Public Property QuestionID As Integer
  Public Property Answers As New List(Of String)
End Class

我在迁移文件中得到了这个:

  CreateTable(
    "dbo.questions",
    Function(c) New With
      {
        .QuestionID = c.Int(nullable:=False, identity:=True)
      }) _
    .PrimaryKey(Function(t) t.QuestionID)

我编写它的方式不会生成字符串列表。希望有人能阐明我在这里缺少的基本概念。

不要创建带有答案的文本字段,不!

您需要创建一个新的 table、答案 table,然后向您的问题 table 添加外键,如下所示:

Public Class question
Public Property QuestionID As Integer
'Public Property Answers As String

Public Overridable Property Answers() as ICollection (Of Answer)
End Class

Public Class Answer
Public Property ID As Integer
Public Property questionID as Integer
Public Property Answers As String

<ForeignKey("questionID")>
Public Overridable Property Question as question

End Class

然后你就这样做 (C#):

IEnumerable<answer>  answers=question1.Answers.ToList();