none
What is EF proxy creation RRS feed

  • Question

  • i heard we use virtual keyword in model class and which help EF to create proxy at run time but i do not know what that proxy will do ?

    a example attached here where virtual keyword has been used. so i like to know if we remove virtual keyword then what operation will not work?

    what people say virtual keyword enable proxy creation. show me with a example that what proxy creation does and how it help. thanks

    public class Author
    {
        [Key]
        public int Id { get; set; }
     
        public string FirstName { get; set; }
     
        public string LastName { get; set; }
     
        public virtual ICollection Books { get; set; }
    }
    
    public class Book
    {
        public int Id { get; set; }
     
        public string Title { get; set; }
     
        public int AuthorId { get; set; }
     
        [ForeignKey(nameof(AuthorId))]
        public virtual Author Author { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new BookStoreContext())
            {
                var author = context.Authors
                    .FirstOrDefault();
               
                foreach (var book in author.Books)
                    Console.WriteLine(book.Title);
            }
            Console.ReadLine();
        }     
    }
    Wednesday, December 6, 2017 2:00 PM

Answers

  • Hi Sudip_inn,

    >>a example attached here where virtual keyword has been used. so i like to know if we remove virtual keyword then what operation will not work?

    Lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook. For example, when using the Author entity class defined below, the related Books will be loaded the first time the Posts navigation property is accessed.

    If you remove virtual keyword, then Lazy loading does not work.

    For Example:

    Turn lazy loading off for serialization
    Lazy loading and serialization don’t mix well, and if you aren’t careful you can end up querying for your entire database just because lazy loading is enabled. Most serializers work by accessing each property on an instance of a type. Property access triggers lazy loading, so more entities get serialized. On those entities properties are accessed, and even more entities are loaded. It’s a good practice to turn lazy loading off before you serialize an entity. The following sections show how to do this.

    Best regards,

    Zhanglong Wu


    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.

    • Marked as answer by Sudip_inn Thursday, December 7, 2017 12:24 PM
    Thursday, December 7, 2017 3:23 AM
    Moderator

All replies

  • Hi Sudip_inn,

    >>a example attached here where virtual keyword has been used. so i like to know if we remove virtual keyword then what operation will not work?

    Lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook. For example, when using the Author entity class defined below, the related Books will be loaded the first time the Posts navigation property is accessed.

    If you remove virtual keyword, then Lazy loading does not work.

    For Example:

    Turn lazy loading off for serialization
    Lazy loading and serialization don’t mix well, and if you aren’t careful you can end up querying for your entire database just because lazy loading is enabled. Most serializers work by accessing each property on an instance of a type. Property access triggers lazy loading, so more entities get serialized. On those entities properties are accessed, and even more entities are loaded. It’s a good practice to turn lazy loading off before you serialize an entity. The following sections show how to do this.

    Best regards,

    Zhanglong Wu


    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.

    • Marked as answer by Sudip_inn Thursday, December 7, 2017 12:24 PM
    Thursday, December 7, 2017 3:23 AM
    Moderator
  • I don't understand this ... 

    If lazy loading and proxying are so tightly bound why can they individually be enabled / disabled?

    Surely to lazy load (by this definition) I need proxying to be enabled?

    Wednesday, July 11, 2018 8:59 AM