EF代码首先映射1到0 w /继承

时间:2012-02-28 17:00:07

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

EF 4.1

我有产品表,产品可以是不同类型,比如说衬衫和裤子。我相信这被称为SuperType和SubType

  

产品(1..1:0)ProductShirt

     

产品(1..1:0)ProductPants

e.g。

Products
--------
ProductId (PK)
Product Name
Product Type (1 or 2, let's say 1 is Shirt, 2 is Pants)

ProductShirtType
----------------
ProductId (PK, FK)
HasShortSleeves
etc....

ProductPantsType
----------------
ProductId (PK, FK)
IsCargoPants
etc...

我试图在EF CF中对此进行建模,并且每种类型的表(TPT)符合这些要求。我遇到的问题是用户可以根据Ladislav Mrnka的回答更改产品类型,这是不可能的,而应该支持组合:

Entity Framework Model with Table-per-Type Inheritance

哪个好,所以现在我的模型看起来像这样:

public class Product {
  public int Id { get; set; }
  public string Name { get; set; }
  public int ProductType { get; set; }
  public virtual ProductSubType SubType {get; set;}
}

public abstract class ProductSubType {
  public int Id { get; set; }
  public virtual Product Product {get; set;}
}

public class ProductShirt : ProductSubType {
  public bool HasShortSleeves { get; set; }
}

public class ProductPants : ProductSubType {
  public bool IsCargoPants { get; set; }
}

我对如何映射抽象类型和子类型感到茫然。同样重要的是我如何从一种产品类型转变为另一种产品类型。

0 个答案:

没有答案