Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First Custom Collections

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
}
like image 738
ScottReynolds Avatar asked Jun 22 '11 19:06

ScottReynolds


1 Answers

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.

like image 67
ScottReynolds Avatar answered Oct 20 '22 05:10

ScottReynolds