EF Code First Custom Collections

*爱你&永不变心* 提交于 2020-01-03 08:45:13

问题


When creating code first collections can you implement a custom class that implements ICollection. The code below is conceptual not actual

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public Category Category { get; set; }
}

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    //Want to Avoid This
    public ICollection<Product> Products { get; set; }
    //Use his instead of above
    public ProductList ProductsInCategory {get;set;}
}
public class ProductsList :ICollection<Product>
{
   public int DiscontinuedProductsCount
   {
        return internalList.Count();
   }
    //Icollection Methods Excluded
}

回答1:


EF can indeed support any collection which inherits from ICollection. We create a deletable collection to support auto deletions and also create collections for child objects to keep the size of our main object smaller.



来源:https://stackoverflow.com/questions/6445522/ef-code-first-custom-collections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!