EF Code First Custom Collections

时间:2011-06-22 19:39:44

标签: entity-framework-4.1 ef-code-first

在创建代码时,您可以实现第一个集合实现ICollection的自定义类。以下代码概念性而非实际

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 个答案:

答案 0 :(得分:7)

EF确实可以支持从ICollection继承的任何集合。我们创建一个可删除的集合来支持自动删除,并为子对象创建集合以使主对象的大小更小。