如何在我的 ASP.NET MVC_CodeFirst 模型中包含一个列表或一个字符串数组
How to have a list or an array of string in my model of ASP.NET MVC_CodeFirst
我想用 Code-first
Database.This 是我的 asp MVC 模型
Model
:
public class Question
{
public virtual int Id { get; set; }
public virtual string Qu { get; set; }
public virtual string Ans { get; set; }
public virtual List<string> KeyWords { get; set; }
}
Question
的每个对象必须有一个 KeyWords
的列表。
这是正确的方法还是如果我有一个 KeyWords
的模型然后在这里创建一个对象关键字列表会更好?
感谢您的帮助,谢谢!
我认为如果您想将数据存储在数据库中,您需要规范化代码。我会这样做:
public class Question
{
public int Id { get; set; }
public string Qu { get; set; }
public string Ans { get; set; }
public virtual List<string> KeyWords { get; set; }
}
public class Keyword
{
public int Id {get;set;}
[ForeignKey("question")]
public int QuestionID {get;set;}
public string keyword {get;set;}
public virtual Question question {get;set;}
}
public QuestionModel :DBContext
{
public virtual DBSet<Question> Questions {get;set;}
public virtual DBSEt<Keywords> Keyword {get;set:}
}
我使用 ICollection<> 而不是虚拟集合列表,但我不确定它是否重要。
我想用 Code-first
Database.This 是我的 asp MVC 模型
Model
:
public class Question
{
public virtual int Id { get; set; }
public virtual string Qu { get; set; }
public virtual string Ans { get; set; }
public virtual List<string> KeyWords { get; set; }
}
Question
的每个对象必须有一个 KeyWords
的列表。
这是正确的方法还是如果我有一个 KeyWords
的模型然后在这里创建一个对象关键字列表会更好?
感谢您的帮助,谢谢!
我认为如果您想将数据存储在数据库中,您需要规范化代码。我会这样做:
public class Question
{
public int Id { get; set; }
public string Qu { get; set; }
public string Ans { get; set; }
public virtual List<string> KeyWords { get; set; }
}
public class Keyword
{
public int Id {get;set;}
[ForeignKey("question")]
public int QuestionID {get;set;}
public string keyword {get;set;}
public virtual Question question {get;set;}
}
public QuestionModel :DBContext
{
public virtual DBSet<Question> Questions {get;set;}
public virtual DBSEt<Keywords> Keyword {get;set:}
}
我使用 ICollection<> 而不是虚拟集合列表,但我不确定它是否重要。