在访问导航属性在EF code第一个错误 [英] Error while accessing the Navigation Property in EF Code First
问题描述
我有一个简单的视图模型具有这样的事情...
视图模型
公共类UserPostCommentViewModel
{ 公共虚拟目录<&评论GT;评论{搞定;组; } 公共虚拟用户用户{搞定;组; } 公共虚拟文章张贴{搞定;组; }
}
,这样我可以返回有1帖子的细节1用户的详细信息和用户提交的许多意见景色....
POST
[重点]
[ScaffoldColumn(真)
公众诠释{帖子ID获取;组; } [必需的(=的ErrorMessage必需)]
[StringLength(250)]
公共字符串名称{搞定;组; } [必需的(=的ErrorMessage必需)]
[数据类型(DataType.MultilineText)
公共字符串车身{搞定;组; } [数据类型(DataType.Date)
公众的DateTime dateCreated会获得{;组; } [数据类型(DataType.Date)
公众的DateTime DateModified {搞定;组; } [ScaffoldColumn(假)]
公众诠释用户名{搞定;组; } [ScaffoldColumn(假)]
公众诠释?标签识别{搞定;组; } 公共虚拟的ICollection<&评论GT;评论{搞定;组; } 公共虚拟用户用户{搞定;组; } 公共虚拟的ICollection<标记和GT;标签{搞定;组; }
注释
[重点]
[ScaffoldColumn(真)
公众诠释CommentID {搞定;组; } [DisplayName的(您的评论)]
[数据类型(DataType.MultilineText)
公共字符串车身{搞定;组; } [数据类型(DataType.Date)
[ScaffoldColumn(真)
公众的DateTime dateCreated会获得{;组; } [ScaffoldColumn(真)
公众诠释{帖子ID获取;组; } [ScaffoldColumn(真)
公众诠释用户名{搞定;组; } 公众用户用户{搞定;组; } 公开发表文章{搞定;组; }
用户
[ScaffoldColumn(真)]
[键]
公众诠释用户名{搞定;组; } [StringLength(15)]
[显示名称(名)]
[必需(的ErrorMessage =第一名称是必需)]
公共字符串名字{获得;组; } [StringLength(15)]
[显示名称(姓氏)]
[必需(的ErrorMessage =最后的名称是必需)]
公共字符串名字{获得;组; } [数据类型(DataType.EmailAddress,的ErrorMessage =请输入有效的电子邮件)]
[显示名称(电子邮件地址)
[必需(的ErrorMessage =必须输入电子邮件)]
[远程(CheckUniqueEmail,用户的ErrorMessage =一个与此电子邮件地址已存在帐户。)
公共字符串电子邮件{获得;组; } [数据类型(DataType.Password)
[必需(的ErrorMessage =需要密码)]
公共字符串密码{搞定;组; } [数据类型(DataType.Date)
[ScaffoldColumn(真)
公众的DateTime JoiningDate {搞定;组; } [数据类型(DataType.Date)
[ScaffoldColumn(真)
公众的DateTime? LastActivityDate {搞定;组; } 公共虚拟的ICollection<邮政和GT;帖子{搞定;组; } 公共虚拟的ICollection<&评论GT;评论{搞定;组; } 公共虚拟的ICollection<标记和GT;标签{搞定;组; }
我有一个StronglyTyped视图与视图模型,这样我可以在一个视图中返回所有三个模特属性...
但:(我想显示的用户名,而不是用户ID在该特定职位的每条评论的前面。所以我使用的视图模型ICollection的意见,这样我可以返回的意见集合。当我尝试访问UserName属性这样@ Model.Comments.User.LastName //或电子邮件//或名字..我得到编译错误。我不能使用这样,因为它集合类型...
我如何使用userid这是在评论可检索的意见实体将用户名。
请帮忙...
得到了所有通过在局部视图中添加工作。
_comments管窥..
在我的部分观点我已经添加了像这样...
========================
@ {
ContextRepository DB =新ContextRepository();
VAR用户ID = Model.UserID;
VAR的userName = db.EUser.Find(用户ID).FirstName;}
您可以创建一个局部视图来显示评论,并呼吁在主视图中的循环中。
帖子详细信息视图
@model UserPostCommentViewModel
@ {
ViewBag.Title =邮报;
布局=〜/查看/共享/ _Layout.cshtml
}@foreach(在Model.Comments评论评论)
{
@ Html.Partial(_评论,评论);
}
_Comment局部视图
@model评论< DIV CLASS =评论>
< DIV CLASS =降序> @ Model.Body< / DIV> < DIV CLASS =中提出,通过> @ Model.User.LastName< / DIV>
< / DIV>
示范问题
您需要让虚拟偷懒的导航性能负载。
公共类评论
{
//其他属性 公共虚拟用户用户{搞定;组; } 公共虚拟邮报帖子{搞定;组; }
}
如果你要那么访问这些导航性能,你需要急于加载它们。否则,你会碰上选择N + 1
问题。使用包含
法急于负载特性
VAR后= db.Posts.Include(P =>评论)
.INCLUDE(P => p.Comments.Select(C => c.User))
。凡(p值=> p.PostID == 1)。单();
I am having a simple ViewModel which have something like this...
ViewModel
public class UserPostCommentViewModel
{
public virtual List<Comment> Comment { get; set; }
public virtual User User { get; set; }
public virtual Post Post { get; set; }
}
so that i can return a view which has 1 Post with the details 1 User details and Many Comments submitted by users....
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; }
Comment
[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; }
USer
[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; }
i have a StronglyTyped View with the ViewModel so that i can return all the three model properties in one view...
BUT :( i want to show the userName instead of userID in front of the each comment for that particular post. so i am using ICollection comments in the viewmodel so that i can return collection of the comments. and when i try to access the userName property like this @Model.Comments.User.LastName // OR Email // OR FirstName .. i am getting the compilation error. and i cant use like this because its a collection type ...
how can i retrieve the userName from the comments entity using the userID which is available in comments..
please help...
Got it all working by adding in the partial view..
_Comments Partial View..
in my partial view i have added like this...
========================
@{
ContextRepository db = new ContextRepository();
var userID = Model.UserID;
var userName = db.EUser.Find(userID).FirstName;
}
You can create a partial view to display the comment and call that inside a loop in the main view.
Post Details view
@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>
Model issues
You need to make the navigational properties virtual to be lazy loaded.
public class Comment
{
//other properties
public virtual User User { get; set; }
public virtual Post Posts { get; set; }
}
If you are going to access these navigational properties then you need to eager load them. Otherwise you will run into Select n+1
problem. Use the Include
method to eager load properties
var post = db.Posts.Include(p => Comments)
.Include(p => p.Comments.Select(c => c.User))
.Where(p => p.PostID == 1).Single();
这篇关于在访问导航属性在EF code第一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!