Entity Framework Basics

I am making an MVC 3 web application and have a weird problem. Here is the code:

Model Announcement:

public class Project
{
    public int ID { get; set; }

    [Required(ErrorMessage = "Write a title.")]
    public string Title { get; set; }

    public DateTime TimeAdded { get; set; }

    [Required(ErrorMessage = "Write some description.")]
    [MaxLength(int.MaxValue)]
    public string Content { get; set; }

    public ICollection<Comment> Comments { get; set; }
}   

public class Comment
{
    public int ID { get; set; }

    [Required()]
    public int ProjectID { get; set; }

    public DateTime TimeAdded { get; set; }

    [Required()]
    public string Text { get; set; }

    public Project project { get; set; }
}

Controller:

public class HomeController : Controller
{
    dataDBContext db = new dataDBContext();

    //
    // GET: /Home

    public ActionResult Index()
    {
        var comments = from c in db.Comments
                       select c;

        var projects = from p in db.Projects
                       orderby p.TimeAdded descending
                       select p;

        return View(projects.ToList());
    }
  • When I start the project Comments do not appear in my view.
  • I set a breakpoint right after both linq queries, debugging and checking the fields of the "project" variables. Comments, they are not filled. Then I check the variable "comments", it has some data. By checking the "project" variable again and the SOMEHOW fields, the Comments are filled in and finally the comments appear on the website. If I do not set a breakpoint and fail to see if the "comment" variables are populated, they will not be displayed on the website. (I hope this is clear).
  • I found a simple way:

    public ActionResult Index()
    {
        var projects = from p in db.Projects
                       orderby p.TimeAdded descending
                       select p;
    
        foreach (var p in projects)
        {
            var comments = from c in db.Comments
                           where c.ProjectID == p.ID
                           select c;
    
    
            p.Comments = comments.ToList();
        }
    
        return View(projects.ToList());
    }
    

    ( 2), :)

?


:

public class HomeController : Controller
{
    dataDBContext db;

    public HomeController()
    {
        db = new dataDBContext();
        db.Configuration.LazyLoadingEnabled = false;
    }

    //
    // GET: /Home

    public ActionResult Index()
    {
        var projects = from p in db.Projects
                       orderby p.TimeAdded descending
                       select p;

        return View(projects.ToList());
    }

. LazyLoadingEnabled. project.ToList(), .

- :

public class HomeController : Controller
{
    dataDBContext db;

    public HomeController()
    {
        db = new dataDBContext();
    }

    //
    // GET: /Home

    public ActionResult Index()
    {
        var projects = from p in db.Projects
                       orderby p.TimeAdded descending
                       select p;

        var comments = from c in db.Comments
                       select c;

        List<Comment> l = comments.ToList();

        return View(projects.ToList());
    }

. ToList() . , . , , ( 3). ?

+3
2

, -

db.ContextOptions.LazyLoadingEnabled = false;
var projects = from p in db.Projects.Include("Comments")
                       orderby p.TimeAdded descending
                       select p;

, .ToList(). "" .

+5

, "ToList", , .

0

Source: https://habr.com/ru/post/1796660/


All Articles