简体   繁体   中英

value cannot be null in Entity Sql Statement

My News.cs class has a one to many relationship with Comment.cs as defined below

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; }
}

I have a controller action where i am trying to fetch one News item alongside all its comments so i set up two viewModels like this

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; }
}

In my Controller action i have

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);

}

The problem is that the debugger is showing that Comment is returning Null whereas in the database there are related comments to that particular news item so i'm getting the error

Value cannot be null. Parameter: source

I've been able to use this code in another project without issues.

I think the problem is because you need to change the Comments collection property as virtual . If you want that related entities be lazy loaded, you need to follow this requirements :

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

Now,If you have disabled lazy loading , another option could be using the Include extension method in your query when you need to find a particular news:

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

This way the related entity will be included in the query result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM