开发者

Entity Framework lazy load

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 e开发者_如何学Pythonnter 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


Maybe you have a problem with your mapping definition. Are you able to do a GetAll on your Category table? Does your Product.Category property knows which Foreign key to use for the Category table?

The article Using DbContext in EF 4.1 Part 6: Loading Related Entities explain different ways to load your entities in eager or lazy mode.

When you use context.Configuration.LazyLoadingEnabled = false, you are defining the global way to load the entities but if you want, you can tell specificly how each properties should be loaded by doing this:

  • Example for lazy loading: public virtual Category Category { get; set; }
  • Example for eager loading: public Category Category { get; set; }

So if it doesnt work for you, i would check your mapping definition and then check the sql query that is sent to your database. You will see exactly if the sql query include your Category details or not.

I know 2 options to see the sql that is generated. You can monitor your database or use Mvc Mini Profiler. If you are using DevArt dotConnect, you have a monitoring tool just for that.


I have just tried to fetch Product object such way:

Product product = (
   from p in repository.Products.Include("Category") 
   where p.ProductID == id 
   select p
).SingleOrDefault(); 

and category was loaded too. This is possible solution. But it is interesting why category is null when I'm using first

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜