Khi tạo mã bộ sưu tập đầu tiên, bạn có thể triển khai lớp tùy chỉnh triển khai ICollection. Mã bên dưới là khái niệm không thực tếMã EF Bộ sưu tập tùy chỉnh đầu tiên
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
}
Bạn đã thử chưa? –