Difference between Public Virtual and List

0

Hello people I would like to understand what is the difference between declaring a model relationship (entity framework) by public List<Modelo> Modelos { get; set; } and public Virtual Modelo Modelos { get; set; } . Thanks again.

    
asked by Max 11.10.2016 в 19:53
source

3 answers

1

Remember that defining something as virtual leaves the possibility of overwriting the functionality

virtual (C # Reference)

that capacity is the one that uses entity framework to act as an intermediary and inject its implementation, so you can add lazy load capacity in the relationships. If you do not define the EF property as virtual, it can not be interposed with which the lazy proxy is not injected into the entity.

Defining a List < > or an entity depends on the navigation you want to obtain, it is clear that being a list will be a one to many relationship. Analyze the example

Configure One-to-Many Relationship

You will see how both properties are defined to have the full navigation, but both must have the virtual

    
answered by 11.10.2016 в 23:04
0

The difference between using virtual or not is that when you specify it, Entity Framework can load related entities with lazy loading

    
answered by 11.10.2016 в 21:05
0

Something that you have to be careful with the virtual ones is that they are dangerous for the performance since they load the related data ALWAYS. In my opinion the best thing is not to declare a virtual property if you think that it will be loaded with a lot of data.

For example, it is as if you do a GET () of a COUNTRY class "china" to simply bring the description and the PAIS class has a property IENUMERABLE<PERSONAS> , if it is with virtual it will bring all the PERSON data. Comes to build a country select by joining with the people table that has millions of records

    
answered by 13.10.2016 в 09:09