EF6 code first to 6NF tables

125 views Asked by At

I have the following database model

Entity
Id, ...

PropertyType
Id, Name, DataType, ...

EntityProperty
Id, EntityId, PropertyTypeId

PropertyValueString
Id, Value

PropertyValueDateTime
ID, Value

I am using EF6 code first to map my entities to this data model

class Entity {
  public Guid Id { get; set; }
  ...
}

class PropertyType {
  public Guid Id { get; set; }
  public string Name { get; set; }
  public Type DataType { get; set; }
  ...
}


class EntityProperty {
  public Guid Id { get; set; }
  public Guid EntityId { get; set; }
  public Entity Entity { get; set; }
  public Guid PropertyTypeId { get; set; }
  public PropertyType PropertyType { get; set; }
  public string StringValue { get; set; }
  public DateTime? DateTimeValue { get; set; }
}

Mapping Entity and PropertyType class to respective tables is straight forward. EntityProperty is mapped using

Map(m => m.ToTable("EntityProperty").Properties(p => new { p.Id, p.EntityId, p.PropertyTypeId });
Map(m => m.ToTable("PropertyValueString").Properties(p => new { p.Id, p.StringValue });
Map(m => m.ToTable("PropertyValueDateTime").Properties(p => new { p.Id, p.DateTimeValue });

How do I map my EntityProperty to PropertyValueString or PropertyValueDateTime tables depending on which field has value? Also, the query generated when including EntityProperty should LEFT JOIN with PropertyValueString and PropertyValueDateTime.

Is this even possible with Entity Framework?

0

There are 0 answers