为什么使用ICollection而不是IEnumerable或List< T>在许多/一对多的关系? [英] Why use ICollection and not IEnumerable or List<T> on many-many/one-many relationships?

查看:135
本文介绍了为什么使用ICollection而不是IEnumerable或List< T>在许多/一对多的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在教程中看到很多,导航属性为 ICollection< T>

I see this a lot in tutorials, with navigation properties as ICollection<T>.

这是实体框架的强制性要求?可以使用 IEnumerable

Is this a mandatory requirement for Entity Framework? Can I use IEnumerable?

使用 ICollection 而不是 IEnumerable 甚至列表< T>

推荐答案

通常你选择的将取决于你需要访问哪些方法。一般来说 - IEnumerable (MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx ),列出只需要迭代的对象, ICollection<> (MSDN: http://msdn.microsoft.com/en-us/library/92t2ye13.aspx ),了解需要迭代和修改的对象列表, List<> 用于需要迭代,修改,排序等的对象列表(请参阅此处的完整列表: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx )。

Usually what you choose will depend on which methods you need access to. In general - IEnumerable<> (MSDN: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx) for a list of objects that only needs to be iterated through, ICollection<> (MSDN: http://msdn.microsoft.com/en-us/library/92t2ye13.aspx) for a list of objects that needs to be iterated through and modified, List<> for a list of objects that needs to be iterated through, modified, sorted, etc (See here for a full list: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx).

从更具体的角度来看,懒惰加载可以选择类型。默认情况下,实体框架中的导航属性带有更改跟踪,并且是代理。为了将动态代理创建为导航属性,虚拟类型必须实现 ICollection

From a more specific standpoint, lazy loading comes in to play with choosing the type. By default, navigation properties in Entity Framework come with change tracking and are proxies. In order for the dynamic proxy to be created as a navigation property, the virtual type must implement ICollection.


表示关系多结尾的导航属性必须返回实现ICollection的类型,其中T是关系另一端的对象类型。 - 创建POCO代理的要求 MSDN

有关定义和管理关系的更多信息 MSDN

这篇关于为什么使用ICollection而不是IEnumerable或List&lt; T&gt;在许多/一对多的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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