I have following model of data for Entity Framework.
I have abstract Product. Every Product relates with one Category of products. For example:
public abstract class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
And there are concrete products:
public class ConcreteProduct1 : Product
{
// some specific member
}
public class ConcreteProduct2 : Product
{
// some specific member
}
//etc.
I have hierarchical Categories, for example:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public Category Parent { get; set; }
public ICollection<Category> Children { get; set; }
public ICollection<Product> Products { get; set; }
}
Every Category has ICollection<Product> Products
.
Problem: Category should be related with only products some concrete product type. I.e. I need be able get Concrete Products into Category, for example:
public Category<ConcreteProduct1> GetCategory<ConcreteProduct1> ()
{
// should return Category that contain ICollection<ConcreteProduct1>
}
How I can describe this restriction in my Entity Framework model? Or may be there are some best practice for building these relations?