我有以下的实体框架数据模型。实体框架:类别/产品关系模型
我有抽象产品。每个产品都涉及一个类别的产品。例如:
public abstract class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
而且有具体的产品:
public class ConcreteProduct1 : Product
{
// some specific member
}
public class ConcreteProduct2 : Product
{
// some specific member
}
//etc.
我有分层分类,例如:
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; }
}
每个类别都有ICollection<Product> Products
。
问题:类别应该只与产品的某些具体产品类型有关。即我需要能够得到混凝土制品分成类别,例如:
public Category<ConcreteProduct1> GetCategory<ConcreteProduct1>()
{
// should return Category that contain ICollection<ConcreteProduct1>
}
我怎么能在我的实体框架模型来描述这个限制?或者可能有一些建立这些关系的最佳做法?
谢谢,但不幸的是,我不明白你的答案:)我的问题与Entity Framewrok模型有关,即它涉及到ORM - 如何在数据库中表示我的域模型,由C#代码通过实体框架代码第一。我的问题 - 我不需要按类别获得父级产品,我需要按类别获得具体产品。我会澄清我的问题。 – karavanjo