【问题标题】:value cannot be null in Entity Sql Statement实体 Sql 语句中的值不能为空
【发布时间】:2015-10-26 00:45:31
【问题描述】:

我的 News.cs 类与 Comment.cs 具有一对多的关系,定义如下

public class News
{
    public int NewsId { get; set; }    
    [Display(Name = "Title")]
    public string Title { get; set; }    
    [Display(Name = "Details")]
    public string Details { get; set; }    
    public DateTime DateCreated { get; set; }    
    public int AppUserId { get; set; }
    [ForeignKey("AppUserId")]
    public virtual AppUser AppUser { get; set; }   
    public ICollection<Comment> Comment { get; set; }   
}
public class Comment
{
    public int CommentId { get; set; }
    public string CommentText { get; set; }
    public DateTime DateCreated { get; set; }
    public int AppUserId  { get; set; }
    public int? NewsId { get; set; }
    [ForeignKey("AppUserId")]
    public virtual AppUser AppUser { get; set; }
    [ForeignKey("NewsId")]
    public virtual News News { get; set; }
}

我有一个控制器操作,我试图在它的所有 cmets 旁边获取一个新闻项目,所以我设置了两个这样的视图模型

public class CommentVM
{    
    public string CommentText { get; set; }
    public DateTime DateCreated { get; set; }
    public string Author { get; set; }    
}
public class NewsCommentsVM
{
    [Display(Name = "Title")]
    public string Title { get; set; }
    [Display(Name = "Details")]
    public string Details { get; set; }
    public DateTime DateCreated { get; set; }
    public string Author { get; set; }
    public List<CommentVM> Comments { get; set; }
}

在我的控制器操作中,我有

public ActionResult Details(int? id)
{
    UOW _unit = new UOW();
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    News news = _unit.NewsRepository.GetByID(id);

    if (news == null)
    {
        return HttpNotFound();
    }
    var model = new NewsCommentsVM()
    {
        Title = news.Title,
        Details = news.Details,
        DateCreated = news.DateCreated,
        Author = news.AppUser.FirstName
        Comments = news.Comment.Select(c => new CommentVM()
        {
            CommentText = c.CommentText,
            Author = c.AppUser.Email,
            DateCreated = c.DateCreated
        }).ToList()

    };
    return View(result);

}

问题是调试器显示 Comment 正在返回 Null,而在数据库中存在与该特定新闻项相关的 cmets,所以我收到错误

值不能为空。参数:来源

我已经能够在另一个项目中使用此代码而没有问题。

【问题讨论】:

    标签: c# asp.net linq entity-framework asp.net-mvc-4


    【解决方案1】:

    我认为问题在于您需要将Comments 集合属性更改为virtual。如果您希望相关实体被延迟加载,您需要关注此requirements

    public class News
    {
        //...  
        public  virtual ICollection<Comment> Comment { get; set; }   
    }
    

    现在,如果您禁用了lazy loading,当您需要查找特定新闻时,另一个选项可能是在查询中使用Include 扩展方法:

    int id=3;
    var specificNews=context.News.Include(n=>n.Comment).FirstOrDefault(n=>n.Id==id);
    

    这样相关实体将包含在查询结果中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-26
      • 2023-03-06
      相关资源
      最近更新 更多