在EF Code First中访问导航属性时出错

时间:2011-09-13 22:13:23

标签: asp.net-mvc entity-framework asp.net-mvc-3 view

我有一个简单的ViewModel,它有类似的东西......

视图模型

public class UserPostCommentViewModel
    {

        public virtual List<Comment> Comment { get; set; }

        public virtual User User { get; set; }

        public virtual Post Post { get; set; }
    }

这样我就可以返回一个包含1个帖子的视图,其中包含1个用户详细信息和用户提交的许多评论....

POST

 [Key]
        [ScaffoldColumn(true)]
        public int PostID { get; set; }

        [Required(ErrorMessage="Required")]
        [StringLength(250)]
        public string Title { get; set; }

        [Required(ErrorMessage="Required")]
        [DataType(DataType.MultilineText)]
        public string Body { get; set; }

        [DataType(DataType.Date)]
        public DateTime DateCreated { get; set; }

        [DataType(DataType.Date)]
        public DateTime DateModified { get; set; }

        [ScaffoldColumn(false)]
        public int UserID { get; set; }

        [ScaffoldColumn(false)]
        public int? TagID { get; set; }

        public virtual ICollection<Comment> Comments { get; set; }

        public virtual User Users { get; set; }

        public virtual ICollection<Tag> Tags { get; set; }

注释

[Key]
        [ScaffoldColumn(true)]
        public int CommentID { get; set; }

        [DisplayName("Your Comment")]
        [DataType(DataType.MultilineText)]
        public string Body { get; set; }

        [DataType(DataType.Date)]
        [ScaffoldColumn(true)]
        public DateTime DateCreated { get; set; }

        [ScaffoldColumn(true)]
        public int PostID { get; set; }

        [ScaffoldColumn(true)]
        public int UserID { get; set; }

        public User User { get; set; }

        public Post Posts { get; set; }

用户

[ScaffoldColumn(true)]
        [Key]
        public int UserID { get; set; }

        [StringLength(15)]
        [DisplayName("First Name")]
        [Required(ErrorMessage="First Name is Required")]
        public string FirstName { get; set; }

        [StringLength(15)]
        [DisplayName("Last Name")]
        [Required(ErrorMessage = "Last Name is Required")]
        public string LastName { get; set; }

        [DataType(DataType.EmailAddress,ErrorMessage="please enter valid email")]
        [DisplayName("Email Address")]
        [Required(ErrorMessage = "Email is Required")]
        [Remote("CheckUniqueEmail","User",ErrorMessage="An account with this email address already exists.")]
        public string Email { get; set; }

        [DataType(DataType.Password)]
        [Required(ErrorMessage = "Password is Required")]
        public string Password { get; set; }

        [DataType(DataType.Date)]
        [ScaffoldColumn(true)]
        public DateTime JoiningDate { get; set; }

        [DataType(DataType.Date)]
        [ScaffoldColumn(true)]
        public DateTime? LastActivityDate { get; set; }

        public virtual ICollection<Post> Posts { get; set; }

        public virtual ICollection<Comment> Comments { get; set; }

        public virtual ICollection<Tag> Tags { get; set; }

我有一个带有ViewModel的StronglyTyped视图,这样我就可以在一个视图中返回所有三个模型属性......

但是:(我想在该特定帖子的每个评论前面显示userName而不是userID。所以我在viewmodel中使用ICollection评论,以便我可以返回评论的集合。当我尝试访问userName属性,如@ Model.Comments.User.LastName //或电子邮件// OR FirstName ..我收到编译错误。我不能这样使用,因为它是一个集合类型......

如何使用评论中提供的userID从评论实体中检索userName。

请帮忙......

通过在局部视图中添加来完成所有工作..

_Comments Partial View ..

在我的部分视图中我添加了这样的内容......

========================

@{
    ContextRepository db = new ContextRepository();
    var userID = Model.UserID;
    var userName = db.EUser.Find(userID).FirstName;   

}

1 个答案:

答案 0 :(得分:0)

您可以创建一个局部视图来显示注释,并在主视图中的循环内调用它。

发布详细信息视图

@model UserPostCommentViewModel
@{
    ViewBag.Title = "Post";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@foreach (Comment comment in Model.Comments)
{
    @Html.Partial("_Comment", comment);
}

_Comment partial view

@model Comment

<div class="comment">
    <div class="desc">@Model.Body</div>

    <div class="submitted-by">@Model.User.LastName</div>
</div>

模型问题

您需要将虚拟导航属性设置为延迟加载。

public class Comment
{
    //other properties

    public virtual User User { get; set; }

    public virtual Post Posts { get; set; }
}

如果您要访问这些导航属性,则需要急切加载它们。否则你会遇到Select n+1问题。使用Include方法预测加载属性

var post = db.Posts.Include(p => Comments)
            .Include(p => p.Comments.Select(c => c.User))
            .Where(p => p.PostID == 1).Single();