为什么EF Code First中的外键标记为虚拟? [英] Why are foreign keys in EF Code First marked as virtual?

查看:349
本文介绍了为什么EF Code First中的外键标记为虚拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public virtual Student Student {get; set;} 

为什么外键约束需要被标记为虚拟?我看到虚拟和缺乏虚拟的例子。是否重要?

解决方案

通过查看: https://msdn.microsoft.com/en-us/library/jj574232(v = vs.113).aspx ,以及@Shoes的评论中提供的链接。



我会说:



<强> 1。如果您虚拟声明属性:



当查询主对象时,您的虚拟属性(默认情况下)将不会立即加载。如果您尝试访问数据库或访问其中的一个组件,它将从数据库中进行检索。



这称为延迟加载。



2。如果您声明它是非虚拟的:



您的属性将(默认情况下)与您的主实体中的所有其他属性一起加载。这意味着您的财产将可以访问:它已被撤回。实体不必重新查询数据库,因为您访问此属性。



这被称为热切加载。



我的意见:



更多的时候,我选择热切地加载(非虚拟),因为大多数时候,我需要每一个实体被使用,而不必回调(更快的情况下,你真的想要一切快),但如果你访问这个属性只有一段时间(你没有列出任何东西),你想要更多的只是其余的信息exept这个,然后使它虚拟,所以这个属性不会减慢其余的查询只是为了一些访问。



希望这是清楚的...

例如:



我不会使用虚拟(Eagerly):

  foreach(查询中的var行)
{
var v = line.NotVirtual; //我访问每一行的财产
}

我会使用虚拟或懒惰加载:

  foreach(查询中的var行)
{
if (line.ID == 509)//因为这个条件
var v = line.Virtual; //我一次访问该属性一次
}

最后一件事: / p>

如果您不查询超过1000行的数据库,那么无论您选择什么都不会有很大的影响。此外,您可以虚拟声明这些属性,如果要测试其他方式,您只需执行以下操作:

  context.LazyLoadingEnabled = false; 

它将取消虚拟效果。


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?

解决方案

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 :

context.LazyLoadingEnabled = false;

It will cancel the virtual effect.

这篇关于为什么EF Code First中的外键标记为虚拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆