Сохранить связь между пользователем и объектом в ASP.NET MVC4

У меня есть объект «Упражнение», определенный в моем веб-приложении ASP.NET MVC4. Я использую аутентификацию с помощью формы с классом 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 для связи между упражнением и вошедшим в систему пользователем? Как я могу получить текущий профиль пользователя в моем контроллере?


person Simon    schedule 17.08.2013    source источник


Ответы (1)


Измените это следующим образом:

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; }         
}
person Amin Saqi    schedule 17.08.2013