none
Entity Framework: Why virtual keyword used to declare properties in DbContext RRS feed

  • Question

  • i rare use EF. so i have a question. if we do not use virtual keyword with dbset then lazy load will not work?

    i read a article from this link https://stackoverflow.com/a/24114284/5314244 they provide code like

    public class AppContext : DbContext
    {
        public AppContext()
        {
            Configuration.LazyLoadingEnabled = true;
        }
    
        public virtual DbSet<AccountType> AccountTypes { get; set; }
    }
    
    public class AccountType
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<AccountCode> AccountCodes { get; set; }
    }
    
    public class AccountCode
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public Guid AccountTypeId { get; set; }
        public virtual AccountType AccountType { get; set; }
    }

    they said :

    The virtual keyword on the navigation properties are used to enable lazy loading mechanism, but the LazyLoadingEnabled property of the configuration must be enabled.
    
    The virtual keyword on AccountType::AccountCodes navigation property will load all account codes the moment there is a programmatically access to that property while the db context are still alive

    if we declare this code without virtual keyword public DbSet<AccountType> AccountTypes { get; set; } then when this line execute var accountCodes = accountType.AccountCodes; what will happen?

    Error will thrown or null will be stored in accountCodes variable?

    second question what is default in EF---> lazy load or eager loading? how many type of loading option available ?

    thanks

    Monday, October 9, 2017 11:57 AM

Answers

  • Hi,

    The differences between Lazy Loading and Eager Loading is Lazy loading use virtual keyword.

    Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

    More about loading specifications in EF ,Please check Entity Framework Loading Related Entities

    In your case, if there is no virtual keyword on navigation properties, when you try to use the collection of AccountCodes , the exception would thrown.

    >>second question what is default in EF---> lazy load or eager loading? how many type of loading option available ?

    Lazy Loading uses a dynamic proxy. By default, if the POCO class satisfies the following two conditions, EF would use Lazy loading:

    • POCO class is public and not sealed.
    • The navigation property is marked as virtual.

    To close Lazy Loading, you can set LazyLoadingEnabled to false, and if the navigation properties are not marked as virtual, Lazy Loading does not work.

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.





    • Edited by Bob Ding Tuesday, October 10, 2017 12:40 PM
    • Marked as answer by Sudip_inn Wednesday, November 1, 2017 8:42 AM
    Tuesday, October 10, 2017 12:38 PM