实体框架4.1 POCO代码首先有什么影响? [英] What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?
问题描述
在EF Code First的属性中使用 virtual
关键字有效吗?有人可以在不同的情况下描述所有的后果吗?
Does the virtual
keyword has an effect when used on the properties in EF Code First?. Can someone describe all of its ramifications in different situations?
例如,我知道它可以控制懒加载 - if您可以在ICollection /一对多关系属性中使用virtual关键字,默认情况下它将被延迟加载,而如果将虚拟关键字退出,则它将被迫切加载。
For instance, I know it can control lazy loading -- if you use the virtual keyword on an ICollection/one-to-many relationship property, it will be lazy-loaded by default, whereas if you leave the virtual keyword out, it will be eager-loaded.
在具有POCO实体的EF中, virtual
关键字有什么其他效果?我应该默认在我的所有属性上使用 virtual
,还是默认使用?
What other effects can virtual
keyword have in EF with POCO entities?. Should I make it default to use virtual
on all my properties, or default to not using it?
推荐答案
到目前为止,我知道这些效果。
So far, I know of these effects.
- Lazy Loading < a>:任何
virtual
ICollections将被懒加载,除非您另有特别标记。 -
更有效率的更改跟踪。如果您符合以下所有要求,则您的更改跟踪可以通过挂起虚拟属性来使用更有效的方法。从链接
- Lazy Loading: Any
virtual
ICollections will be lazy-loaded unless you specifically mark them otherwise. More efficient change tracking. If you meet all the following requirements then your change tracking can use a more efficient method by hooking your virtual properties. From the link:
要获得更改跟踪代理,
基本规则是您的课程必须是
public ,非抽象或非密封。
您的类还必须为持久化的所有
属性实现public
virtual getters / setters。
最后,您必须仅将
的关系导航
属性声明为ICollection< T>
。
它们不能是一个具体的
实现或另一个接口
源自ICollection< T>
(a
与延迟加载
代理)
To get change tracking proxies, the basic rule is that your class must be public, non-abstract or non-sealed. Your class must also implement public virtual getters/setters for all properties that are persisted. Finally, you must declare collection based relationship navigation properties as
ICollection<T>
only. They cannot be a concrete implementation or another interface that derives fromICollection<T>
(a difference from the Deferred Loading proxy)
描述这一点的另一个有用的链接是MSDN 创建POCO代理的要求。
Another useful link describing this is MSDN's Requirements for Creating POCO Proxies.
这篇关于实体框架4.1 POCO代码首先有什么影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!