0

Using fluent api, how do I map multiple properties of the same data type to a single table conditionally.

Database Model: DB Model

  • ListType would include the Grouping names i.e. Allergens
  • ListItem would include all possible values for the given type
  • ProductListItem contains all "selected" values for a given product.

The goal is to use the ProductListItem table and apply it across multiple properties of a model (of the same ProductListItem type) based on the ListType (WHERE ProductListItem.ListTypeID = 1).

public class Product
{
    public int ProductID { get; set; }
    public List<ProductListItem> Allergens { get; set; }
    public List<ProductListItem> DoesNotContain { get; set; }
}
alex.davis.dev
  • 411
  • 7
  • 18

1 Answers1

0

I really don't think that you can achieve this with conditional mapping, but you can cheat.

 public List<ProductListItem> Allergens 
 { 
    get { return this.ProductListItems.Where(i => i.ListType.Name=="Allergens").ToList=();}
 }

Or can you can optionally create a single class for different ListItems with the same baseclass and use the TPH mapping: http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph

The code would be something like this:

class Product
{
  public List<AllergenProductListItem> Allergens { get; set; }
  public List<DoesNotContainListItem> DoesNotContain { get; set; }
}

Its obviously not dynamic regarding the number of the item types (hard to add a new one), but neither is you desired solution, since if you want to have a new type you should modify the code.

Tamás Varga
  • 635
  • 5
  • 17