我使用实体框架代码首先,我已经下一个POCO类:POCO实体框架和多态性
public class Product
{
public int ProductID { get; set; }
public int Title{ get; set; }
public int Price { get; set; }
public int ProductCategoryID { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
}
public class ProductCategory
{
public int ProductCategoryID { get; set; }
public int Title{ get; set; }
public virtual ICollection<Product> Products { get; set; }
}
因此,让产品有一个简单的方法MakeSale
取决于ProductCategory
public int void GiveSale()
{
//I can do like this ...
switch (this.ProductCategoryID)
{
case 1: return this.Price * 0,8;
break;
case 2: return 0;
...
}
}
但我不喜欢这种方法,我想用OOP并且有类似的东西:
public class Dress : Product
{
public override int void GiveSale()
{
return this.Price * 0,8;
}
}
public class Shoes : Product
{
public override int void GiveSale()
{
return 0;
}
}
如何使用实体框架来实现这一点,并保存与我的实体的易用性?