none
Lazy Loading in entities code first RRS feed

  • Question

  • Hi,

      I've searched all over google looking for a decent example on how to do Lazy

    Loading using code first in entities (real *code first* code first, where you make your

    poco classes and use those to create your database) framework, but I have yet

    to find one.  Can anyone either point me to a complete explanation of how this is

    done on the internet, or explain it to me in a posted reply?  

    - James

    Wednesday, December 5, 2012 5:07 PM

Answers

  • Hi James;

    Your navigation properties must be marked with virtual keyword this tells Entity Framework to create the proxy classes which supports lazy loading, this is for both collections and individual entity classes. Although using List<...> for the collections I understand that using ICollection<...> has more benefits.

    [Serializable]
    public class Application_
    {
        [Key]
        public int ApplicationID { get; set; }
        public DateTime SubmittedDate { get; set; }
        // Modified the next line
        public virtual ICollection<Individual> Individuals { get; set; }
    }
    
    [Serializable]
    public class Individual
    {
        [Key]
        public int IndividualID { get; set; }
        [ForeignKey("Application_")]
        public int ApplicationID { get; set; }
        // Modified the next line
        public virtual Application_ Application_ { get; set; }
        // Modified the next line
        public virtual ICollection<IndividualBenefit> IndividualBenefits{ get; set; }
        [Required]
        [StringLength(15, ErrorMessage = "Name must be no more than 15 characters long.")]
        public string FirstName { get; set; }
        [Required]
        [StringLength(21, ErrorMessage = "Name must be no more than 21 characters long.")]
        public string LastName { get; set; }
        public bool IsHeadOfHousehold { get; set; }
        [Required(ErrorMessage = "SSN required.")]
        public string SSN { get; set; }
        public DateTime DOB { get; set; }
        [StringLength(1)]
        public string Gender { get; set; }
    }
    
    [Serializable]
    public class IndividualBenefit
    {
        public object B;
        [Key]
        public int BenefitID { get; set; }
        [ForeignKey("Individual")]
        public int IndividualID { get; set; }
        // Modified the next line    
        public virtual Individual Individual { get; set; }
        [Required]
        [StringLength(3)]
        public string BenefitCode { get; set; }
    }
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by jamesiw Wednesday, December 5, 2012 10:00 PM
    Wednesday, December 5, 2012 7:12 PM

All replies

  • Sorry, maybe I wasn't specific enough..

    I meant I couldn't find an example that would explain how to do lazy loading with the kind of

    code I am using.  I would like to know how to modify *my* code to make it work with lazy loading..

    if anyone can tell me how.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    
    
    namespace JamesPoco
    {
        [Serializable]
        public class Application_
        {
            [Key]
            public int ApplicationID { get; set; }
            public DateTime SubmittedDate { get; set; }
            public virtual <List<Individual> Individuals { get; set; }
        }
    
        [Serializable]
        public class Individual
        {
            [Key]
            public int IndividualID { get; set; }
            [ForeignKey("Application_")]
            public int ApplicationID { get; set; }
            public Application_ Application_ { get; set; }
            public virtual <List<IndividualBenefit> IndividualBenefits{ get; set; }
            [Required]
            [StringLength(15, ErrorMessage = "Name must be no more than 15 characters long.")]
            public string FirstName { get; set; }
            [Required]
            [StringLength(21, ErrorMessage = "Name must be no more than 21 characters long.")]
            public string LastName { get; set; }
            public bool IsHeadOfHousehold { get; set; }
            [Required(ErrorMessage = "SSN required.")]
            public string SSN { get; set; }
            public DateTime DOB { get; set; }
            [StringLength(1)]
            public string Gender { get; set; }
        }
    
        [Serializable]
        public class IndividualBenefit
        {
            public object B;
            [Key]
            public int BenefitID { get; set; }
            [ForeignKey("Individual")]
            public int IndividualID { get; set; }
            public Individual Individual { get; set; }
            [Required]
            [StringLength(3)]
            public string BenefitCode { get; set; }
        }
    
        public class BenefitsContext : DbContext
        {
            public BenefitsContext() : base("ApplyForBenefitsDatabase") { }
            public DbSet<Application_> Applications { get; set; }
            public DbSet<Individual> Individuals { get; set; }
            public DbSet<IndividualBenefit> IndividualBenefits { get; set; }
        }
    }
    

    I also want to be able to iterate through the "lazy" list, but I tried doing some lazy loading examples

    and was confused about how to implement it, because the code in the example is different than mine..

    I noticed that you can't go through "lazy" with foreach so does this mean "lazy" has to be converted

    to a list somehow?  I think this is what the example I saw was doing, but I didn't quite understand.


    Wednesday, December 5, 2012 5:56 PM
  • Hi James;

    Your navigation properties must be marked with virtual keyword this tells Entity Framework to create the proxy classes which supports lazy loading, this is for both collections and individual entity classes. Although using List<...> for the collections I understand that using ICollection<...> has more benefits.

    [Serializable]
    public class Application_
    {
        [Key]
        public int ApplicationID { get; set; }
        public DateTime SubmittedDate { get; set; }
        // Modified the next line
        public virtual ICollection<Individual> Individuals { get; set; }
    }
    
    [Serializable]
    public class Individual
    {
        [Key]
        public int IndividualID { get; set; }
        [ForeignKey("Application_")]
        public int ApplicationID { get; set; }
        // Modified the next line
        public virtual Application_ Application_ { get; set; }
        // Modified the next line
        public virtual ICollection<IndividualBenefit> IndividualBenefits{ get; set; }
        [Required]
        [StringLength(15, ErrorMessage = "Name must be no more than 15 characters long.")]
        public string FirstName { get; set; }
        [Required]
        [StringLength(21, ErrorMessage = "Name must be no more than 21 characters long.")]
        public string LastName { get; set; }
        public bool IsHeadOfHousehold { get; set; }
        [Required(ErrorMessage = "SSN required.")]
        public string SSN { get; set; }
        public DateTime DOB { get; set; }
        [StringLength(1)]
        public string Gender { get; set; }
    }
    
    [Serializable]
    public class IndividualBenefit
    {
        public object B;
        [Key]
        public int BenefitID { get; set; }
        [ForeignKey("Individual")]
        public int IndividualID { get; set; }
        // Modified the next line    
        public virtual Individual Individual { get; set; }
        [Required]
        [StringLength(3)]
        public string BenefitCode { get; set; }
    }
    

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by jamesiw Wednesday, December 5, 2012 10:00 PM
    Wednesday, December 5, 2012 7:12 PM