What is the difference between Virtual & List in below lines. I am using Ado.Net Entity Model
public virtual ICollection<Employee> Employees { get; set; }
public List<Employee> Employees { get; set; }
The virtual keyword is identifies base members that can be overridden by a derived type. See the the C# language reference to learn about the "virtual" keyword.
MyModel model = dbContext.MyModel.FirstOrDefault(a => a.Id == id); // the Employees will not be loaded EMP e=model.Employees; //Employees loaded
Eager loading :
var model= await dbContext.MyModel //whole MyModel will be loaded .Include(s => s.currency) .ToList();
Best Regards,
Jerry Cai
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
46 Points
179 Posts
Difference
Feb 17, 2021 02:16 PM|jagjit saini|LINK
Hi
What is the difference between Virtual & List in below lines. I am using Ado.Net Entity Model
public virtual ICollection<Employee> Employees { get; set; }
public List<Employee> Employees { get; set; }
Thanks
All-Star
53001 Points
23594 Posts
Re: Difference
Feb 17, 2021 02:53 PM|mgebhard|LINK
The virtual keyword is identifies base members that can be overridden by a derived type. See the the C# language reference to learn about the "virtual" keyword.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/virtual
And see the C# programming guide to learn inheritance fundamentals.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism
Participant
1620 Points
927 Posts
Re: Difference
Feb 17, 2021 10:40 PM|PaulTheSmith|LINK
Do you have a problem? What result are you trying to achieve?
Member
330 Points
232 Posts
Re: Difference
Feb 18, 2021 12:40 AM|SurferOnWww|LINK
“Virtual and List” is not a matter to discuss about difference.
I understand that the above code define an entity class of Entity Framework. As for the “virtual” read the following document:
Why Navigation Properties are virtual by default in EF
As for the “List” see below:
List<T> Class
Participant
970 Points
315 Posts
Re: Difference
Feb 18, 2021 07:56 AM|Jerry Cai|LINK
Hi,jagjit saini
If you do not apply other functions, the 'virtual ICollection will be lazy-loaded. Lazy-loading will make related objects (child objects) are not
loaded automatically with its parent object until they are requested. The List without the virtual, it uses eager loading while with virtual, it can
use both. You can check the difference:
Lazy loading:
Eager loading :
Best Regards,
Jerry Cai