Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are foreign keys in EF Code First marked as virtual?

public virtual Student Student {get; set;}

Why does a foreign key constraint need to be marked as virtual? I've seen examples with both virtual and lacking virtual. Does it matter?

like image 480
Amanda_Panda Avatar asked Nov 21 '16 20:11

Amanda_Panda


People also ask

What does virtual mean in Entity Framework?

In Entity Framework, properties are declared as virtual to allow for change-tracking, and for lazy-loading of related entities. EF dynamically generates a proxy class derived from your POCO class to add these features, and needs to override the properties to do so.

Why navigation properties are virtual?

It enables Entity Framework to avoid loading an entire tree of dependent objects which are not needed from the database.

Can a Foreign Key be null EF core?

If the data type of GradeId is nullable integer, then it will create a null foreign key.

What is Foreign Key in code first approach?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship.


1 Answers

By looking at this : https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx, and the link that has been provided in the comments by @Shoes.

I would say this :

1. if you declare your property virtual :

Your virtual property (by default) won't be loaded right away when querying the main object. It will be retreive from the database ONLY if you try to access it, or access one of it's components.

And this is called lazy loading.

2. if you declare it non-virtual :

Your property will (by default) be loaded right away along with all the other property in your main entity. This means your property will be ready to access : it has already been retreived. Entity won't have to query again the database because you access this property.

This is called eagerly loading.

My opinion :

More often i choose eagerly loading (non-virtual) because most of the time, i need every property of every entity to be used along without having to query back (faster in the case you really want everything quick) but if you access this property only once in a while (your not listing anything) and you want more often just the rest of the informations exept THIS one, then make it virtual so this property won't slow down the rest of the query just for a few access.

Hope this was clear...

Exemples :

Where I would NOT use virtual (Eagerly) :

foreach(var line in query)
{
    var v = line.NotVirtual; // I access the property for every line
}

Where I would use virtual or lazy loading :

foreach(var line in query)
{
    if(line.ID == 509)        // because of this condition
    var v = line.Virtual; // I access the property only once in a while
}

one last thing :

If you don't query over 1 000 lines of a database, then whatever you choose won't have a big effect. Also, you can declare these property virtual and if you want to test the other way around, you just have to do this (Entity 4.0) :

context.LazyLoadingEnabled = false;

It will cancel the virtual effect.

Edit

For newer versions of EF :

WhateverEntities db = new WhateverEntities() 
db.Configuration.LazyLoadingEnabled = false;
like image 136
Antoine Pelletier Avatar answered Sep 19 '22 14:09

Antoine Pelletier