我每次运行时都会遇到此异常,并尝试创建帖子..
我有一个简单的用户,帖子,评论,标签实体。有我定义的关系关联。当我检查innerException它说"Cannot insert the value NULL into column 'CommentID', table " ...
并指向我的上下文,我有一个关系定义..请检查我的关系,并帮助我找到这个我已经声明,在评论ID中不允许null。
modelBuilder.Entity<User>().HasMany(p => p.Posts).WithRequired(u => u.Users).HasForeignKey(P=>P.UserID);
modelBuilder.Entity<User>().HasMany(t => t.Tags).WithRequired(u => u.Users);
modelBuilder.Entity<User>().HasMany(c => c.Comments).WithRequired(u => u.Users);
modelBuilder.Entity<Post>().HasRequired(u => u.Users).WithMany(p=>p.Posts);
modelBuilder.Entity<Post>().HasMany(t => t.Tags).WithMany(p => p.Posts);
modelBuilder.Entity<Post>().HasOptional(c => c.Comments);
modelBuilder.Entity<Comment>().HasRequired(u => u.Users);
modelBuilder.Entity<Comment>().HasRequired(p => p.Posts);
modelBuilder.Entity<Tag>().HasRequired(u => u.Users);
modelBuilder.Entity<Tag>().HasMany(p => p.Posts).WithMany(t => t.Tags);
[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; }
[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? CommentID { 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")]
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; }
[Key]
[ScaffoldColumn(true)]
public int TagID { get; set; }
[StringLength(15)]
public string Name { get; set; }
[ScaffoldColumn(true)]
public int UserID { get; set; }
public User Users { get; set; }
public ICollection<Post> Posts { get; set; }
答案 0 :(得分:0)
您需要在此处发布模型以了解更多详细信息。但我可以看到您的模型配置存在一些问题。 正如你所说,
1 comment can have 1 user, 1 post
那么在Comment
modelBuilder.Entity<Comment>().HasRequired(u => u.Users);
它应该像这样
modelBuilder.Entity<Comment>().HasRequired(u => u.User);
,这也适用于Post
。