尝试学习MVC并且我有点卡在外键上。试图制作简单的Facebook风格发帖系统。
用户,帖子和评论。
可以有很多用户,每个用户可以有很多帖子到所有其他用户,每个帖子可以有很多评论。
Posts To Comments关系工作正常(我假设由于命名约定)。但是,用户发布关系似乎不起作用,当尝试添加用户时我得到了错误。
INSERT语句与FOREIGN KEY约束MVC
冲突我在这里做错了什么,请有人指出我正确的方向。 (另外我知道我的数据库结构很糟糕,我只是想在这里学习MVC)。
USER
namespace Conference.Models
{
public class User
{
public Int32 UserID { get; set; }
public string Username { get; set; }
[ForeignKey("PostedToID")]
public virtual List<Post> Posts { get; set; }
}
}
POST
public class Post
{
[Key]
public Int32 PostID { get; set; }
//to get the name of the user who posted the post
public Int32 UserID { get; set; }
//to get the wall the post was posted to
public Int32 PostedToID { get; set; }
public DateTime PostDateTime { get; set; }
[Required(ErrorMessage = "{0} Is Required")]
[Display(Name = "Post Content")]
public String PostContent { get; set; }
public virtual List<Comment> Comments { get; set; }
}
}
COMMENT
public class Comment
{
public Int32 CommentID { get; set; }
//to get the id of the user that posted the comment
public Int32 UserID { get; set; }
public Int32 PostID { get; set; }
public DateTime CommentDateTime { get; set; }
[Required(ErrorMessage = "{0} Is Required")]
[Display(Name = "Comment Content")]
public String CommentContent { get; set; }
}