【发布时间】:2021-10-28 00:18:54
【问题描述】:
我正在使用 asp.net mvc,对于数据部分,我使用的是代码优先方法,并且我有一个名为“Comment”的模型类。我想实现嵌套的 cmets(我想要回复选项)。这是我的模型类:
public class Comment
{
[Key]
public int CommentID { get; set; }
public int PostID { get; set; }
//if it is null (or zero) , it is a father comment
//if it is not null , it is a boy comment
public int ParentID { get; set; }
[Display(Name = "نام")]
[Required(ErrorMessage = "لطفا نام خود را وارد کنید")]
[DataType(DataType.Text)]
[MaxLength(200)]
public string CommentName { get; set; }
[Display(Name = "ایمیل")]
[Required(ErrorMessage = "لطفا ایمیل خود را وارد کنید")]
[DataType(DataType.Text)]
[MaxLength(200)]
public string CommentEmail { get; set; }
[Display(Name = "متن نظر")]
[Required(ErrorMessage = "لطفا نظر خودرا بنویسید")]
[DataType(DataType.MultilineText)]
public string CommentText { get; set; }
public DateTime CommentDate { get; set; }
public bool IsCommentOk { get; set; }
public virtual Post Post { get; set; }
public virtual Comment Comment1 { get; set; }
public virtual Lis<Comment> Comment2 { get; set; }
public Comment()
{
}
}
我想在 'ParrentID' 和 'CommentID' 之间建立关系。如果 ParentID 为 0 ,它是普通评论,但如果它不为零,它正在回复评论,它的值显示其父评论的 commentID。我使用此代码实现了这个想法:
public virtual Comment Comment1 { get; set; }
public virtual List<Comment> Comment2 { get; set; }
但这不起作用。有什么问题,解决方法是什么?
【问题讨论】:
标签: c# asp.net asp.net-mvc entity-framework-6