[英]Making a basic ASP.NET MVC 5 model
I'm modelling a simple ASP.NET MVC 5 application for learning purposes. 我正在为学习目的建模一个简单的ASP.NET MVC 5应用程序。
A user should be able ask and answer questions (A user can only give one answer to a question). 用户应该能够提出并回答问题(用户只能对一个问题给出一个答案)。 How should this be done?
应该怎么做?
User 1 --> * Answer * --> 1 Question
Because this is my first time modelling with this framework I'm not sure if everything is efficient and if I make good user of the [] statements, should I make adjustments? 因为这是我第一次使用此框架进行建模,所以我不确定一切是否高效,并且如果我很好地使用了[]语句,是否应该进行调整?
User: 用户:
public class User
{
[Key]
public int UserID { get; set; }
...
public virtual ICollection<Question> Questions { get; set; }
public virtual ICollection<Answer> Answers{ get; set; }
}
Question: 题:
public class Question
{
[Key]
public int QuestionID { get; set; }
[Required]
public int UserID { get; set; }
....
}
Answer: 回答:
public class Answer
{
[Key]
public int AnswerID{ get; set; }
[Required]
public int UserID { get; set; }
[Required]
public int QuestionID { get; set; }
public virtual User User { get; set; }
public virtual Question Question { get; set; }
}
Here is a good example for this. 这是一个很好的例子。
You should use Index attribute with the same name. 您应该使用具有相同名称的Index属性。 Such as
如
public class Question
{
[Index("IX_QuestionAndUser", 1, IsUnique = true)]
public int QuestionID { get; set; }
[Index("IX_QuestionAndUser", 1, IsUnique = true)]
public int UserID { get; set; }
}
As you will be going with Entity framework. 正如您将要使用实体框架一样。 Following are the useful resources for relation ship in code first models.
以下是有关代码优先模型中关系的有用资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.