2

I want to know when we use the key word virtual with navigation properties (I learnt it's for lazy loading) but I'm reading a tutorial in https://docs.asp.net/en/latest/data/ef-mvc/intro.html that creates an asp.net web application core and they are not using that virtual anymore. I checked on old versions (MVC4, MVC5) it is always there but not in the core. Can anyone explain to me why?

ekad
  • 13,718
  • 26
  • 42
  • 44

1 Answers1

2

You use virtual properties on entities, so Entity Framework can create a proxy class at runtime that inherits from your entity and injects a stub into overridden properties. This stub does a database call when you access the property's getter from code.

Entity Framework Core does not support lazy loading (yet, and probably never will), so there's no reason for it to mark properties as virtual.

See also: Loading Related Data - Entity Framework Core 1.0.0 Documentation in the officical documentation, Lazy Loading · Issue #3797 · aspnet/EntityFramework · GitHub on GitHub and Why use 'virtual' for class properties in Entity Framework model definitions? here on Stack Overflow.

Community
  • 1
  • 1
CodeCaster
  • 131,656
  • 19
  • 190
  • 236
  • 1
    `probably never will` - Why? Do you have any sources? – Maarten Oct 14 '16 at 11:36
  • 1
    @Maarten the [roadmap does mention it](https://github.com/aspnet/EntityFramework/wiki/Roadmap), but Rowan Miller (and others, IIRC) mentioned they're [_"still undecided..."_ about it](https://github.com/aspnet/EntityFramework/issues/3797#issuecomment-158479567). It's not a definitive "no", but it's not certain it'll ever be implemented either. – CodeCaster Oct 14 '16 at 11:40