Last post Sep 01, 2016 03:25 PM by Mikesdotnetting
Sep 01, 2016 11:32 AM|sudip_inn|LINK
i like to understand the situation where people has to use ICollection and Virtual keyword in EF. so please give me some easy sample code where ICollection and Virtual keyword usage should be there and good explanation should there why and when people should
use ICollection and Virtual keyword. thanks
Sep 01, 2016 11:53 AM|balu.devara|LINK
Sep 01, 2016 01:39 PM|Mikesdotnetting|LINK
ICollection represents a type of collection. specifying a collection as an ICollection allows you to use any type of collection in your code that implements the ICollection interface. In EF sample code, you often see the collection being instantiated in
the constructor of the entity as a HashSet<T>, which implements ICollection, or as a List<T>, which also implements ICollection. ICollection is required if you want to implement Lazy Loading.
The virtual keyword on a collection property allows EF to use it as a base class for dynamic proxies, which are used for lazy loading. Lazy loading is not generally used in web development, so you can leave the virtual keyword out quite safely.
Sep 01, 2016 02:03 PM|sudip_inn|LINK
@Mike can u plzz post a sample code which show why we need icollection instead of list<> and when and why we need virtual keyword in class for EF to create relation
virtual keyword is used to create foreign key relation ?
Sep 01, 2016 03:25 PM|Mikesdotnetting|LINK
As I said, you need virtual ICollection to facilitate lazy loading. Try reading these articles for an explanation of Lazy Loading: