在ASP.NET MVC4中保存用户和实体之间的关系

时间:2013-08-17 11:43:45

标签: asp.net asp.net-mvc-4 form-authentication

我在ASP.NET MVC4 Web应用程序中定义了一个Exercise实体。 我正在使用表单身份验证和默认的AccountModels.cs类。

我的课程看起来像

public class Exercise
    {

        private DateTime _DateCreated = DateTime.Now;
        private UserProfile _Teacher;
        public int Id{ get; set; }
        public string Question { get; set; }
        public int Anwser { get; set; }
        public string Category { get; set; }
        public int maxNbrOfAttempts { get; set; }
        public string Hints { get; set; }
        public virtual ICollection<Quiz> Quizzes { get; set; }

        public DateTime Date
        {
            get { return _DateCreated; }
            set { _DateCreated = value; }
        }

        public UserProfile Author
        {
            get { return _Teacher; }
            set { _Teacher = value; }
        }

    }

我是否正确使用UserProfile链接练习和登录用户? 如何在控制器中获取当前的UserProfile?

1 个答案:

答案 0 :(得分:0)

像这样改变:

public class Exercise
{
    public Exercise()
    {
        this.Date = DateTime.Now;
        this.Author = User.Identity.Name; //Write this line if you want to set
                                          //the currently logged in user as the Author
    public int Id{ get; set; }
    public string Question { get; set; }
    public int Anwser { get; set; }
    public string Category { get; set; }
    public int maxNbrOfAttempts { get; set; }
    public string Hints { get; set; }

    public virtual ICollection<Quiz> Quizzes { get; set; }

    public virtual DateTime Date { get; set; }        
    public virtual UserProfile Author { get; set; }         
}