I have two entities:
public class Product
{
[HiddenInput(DisplayValue=false)]
public int ProductID { get; set; }
[Required(ErrorMessage="Please enter a product name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter a description")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
[Required]
[Range(0.01,double.MaxValue,ErrorMessage="Please enter positive price")]
public decimal Price { get; set; }
public Category Category { get; set; }
[HiddenInput(DisplayValue= false)]
public string ImageFileName { get; set; }
[HiddenInput(DisplayValue = false)]
public string ImageMimeType { get; set; }
}
and
public class Category
{
[HiddenInput(DisplayValue=false)]
public int CategoryID { get; set; }
[Required(ErrorMessage="Please enter a category name")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter a description")]
[DataType(DataType.MultilineText)]
public string Description { get; set; }
public ICollection<Product> Products { get; set; }
[HiddenInput(DisplayValue= false)]
public string ImageFileName { get; set; }
[HiddenInput(DisplayValue = false)]
public string ImageMimeType { get; set; }
}
When I'm trying to get product this way
Product product = repository.Products.FirstOrDefault(p => p.ProductID == id);
Category field is null.
There is no product.Category.Load() and repository.Products.Include("Category")... methods.
context.Configuration.LazyLoadingEnabled = false;
does not affect. Context is an object of next class
public class EFDbContext:DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
How I should load needed field?
Thanks