Search code examples
c#asp.netmodel-view-controllermodel

ASP.NET MVC: how to use Include tag


Can any body explain how to use Include()? I see we hae Images list in model, is it nessesary ti include them in db?

public IEnumerable<Product> GetAll(int Productstate)
    {
        return db.Products.Include("Tags").Include("Images").Include("Category").Include("Format").Where(pl => pl.state > 0).Where(p=>p.state >= Productstate);
    }

public Product Get(Int64 id)
{
    return db.Products.Include("Images").Include("Category").Where(PPR => PPR.Id == id).FirstOrDefault();
}

Product model

 public Int64 Id { get; set; }
    public List<ProdImage> Images { get; set; }
    [MaxLength(20)]
    public String Color { get; set; }        
    public Int64 CustomPropertyId { get; set; }        
    public DateTime CreateTime { get; set; }

Solution

  • What Include would do here, is fill the List<ProdImage> Images, with the images that have a foreign key linking to the product. Otherwise this list would be empty.

    It is not required. If you want to use those images, then you should include them.