簡體   English   中英

制作基本的ASP.NET MVC 5模型

[英]Making a basic ASP.NET MVC 5 model

我正在為學習目的建模一個簡單的ASP.NET MVC 5應用程序。

用戶應該能夠提出並回答問題(用戶只能對一個問題給出一個答案)。 應該怎么做?

User 1 --> * Answer * --> 1 Question

因為這是我第一次使用此框架進行建模,所以我不確定一切是否高效,並且如果我很好地使用了[]語句,是否應該進行調整?

用戶:

public class User
{
    [Key]
    public int UserID { get; set; }
    ...

    public virtual ICollection<Question> Questions { get; set; }
    public virtual ICollection<Answer> Answers{ get; set; }
}

題:

public class Question
{
    [Key]
    public int QuestionID { get; set; }

    [Required]
    public int UserID { get; set; }

    ....
}

回答:

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; }
}

是一個很好的例子。

您應該使用具有相同名稱的Index屬性。

 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; }

       }

正如您將要使用實體框架一樣。 以下是有關代碼優先模型中關系的有用資源。

實體框架中的關系

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM