Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架fluentapi如何有条件地将多个属性映射到单个表_C#_Entity Framework_Ef Fluent Api - Fatal编程技术网

C# 实体框架fluentapi如何有条件地将多个属性映射到单个表

C# 实体框架fluentapi如何有条件地将多个属性映射到单个表,c#,entity-framework,ef-fluent-api,C#,Entity Framework,Ef Fluent Api,使用fluent api,如何有条件地将同一数据类型的多个属性映射到单个表 数据库模型: 列表类型将包括分组名称,即过敏原 ListItem将包括给定类型的所有可能值 ProductListItem包含给定产品的所有“选定”值 目标是使用ProductListItem表,并基于ListType(其中ProductListItem.ListTypeID=1)跨模型(具有相同ProductListItem类型)的多个属性应用它 公共类产品 { public int ProductID{get;

使用fluent api,如何有条件地将同一数据类型的多个属性映射到单个表

数据库模型:

  • 列表类型将包括分组名称,即过敏原
  • ListItem将包括给定类型的所有可能值
  • ProductListItem包含给定产品的所有“选定”值
目标是使用ProductListItem表,并基于ListType(其中ProductListItem.ListTypeID=1)跨模型(具有相同ProductListItem类型)的多个属性应用它

公共类产品
{
public int ProductID{get;set;}
公开列出过敏原{get;set;}
公共列表不包含{get;set;}
}

我真的不认为使用条件映射可以实现这一点,但可以作弊

 public List<ProductListItem> Allergens 
 { 
    get { return this.ProductListItems.Where(i => i.ListType.Name=="Allergens").ToList=();}
 }
公开列出过敏原
{ 
获取{返回this.ProductListItems.Where(i=>i.ListType.Name==“过敏原”).ToList=();}
}
或者,您可以选择为具有相同基类的不同ListItems创建单个类,并使用TPH映射:

代码如下所示:

class Product
{
  public List<AllergenProductListItem> Allergens { get; set; }
  public List<DoesNotContainListItem> DoesNotContain { get; set; }
}
类产品
{
公开列出过敏原{get;set;}
公共列表不包含{get;set;}
}

关于项目类型的数量,这显然不是动态的(很难添加一个新的),但也不是您想要的解决方案,因为如果您想要一个新类型,您应该修改代码。

TPH方法正是我需要走的路线。谢谢你,先生。
class Product
{
  public List<AllergenProductListItem> Allergens { get; set; }
  public List<DoesNotContainListItem> DoesNotContain { get; set; }
}