我有以下数据库模型
Entity
Id, ...
PropertyType
Id, Name, DataType, ...
EntityProperty
Id, EntityId, PropertyTypeId
PropertyValueString
Id, Value
PropertyValueDateTime
ID, Value
我首先使用 EF6 代码将我的实体映射到此数据模型
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; }
}
将 Entity 和 PropertyType 类映射到各自的表是直截了当的。EntityProperty 使用映射
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 });
如何根据哪个字段具有值将我的 EntityProperty 映射到PropertyValueString
或表?PropertyValueDateTime
此外,包含 EntityProperty 时生成的查询应该LEFT JOIN
使用 PropertyValueString 和 PropertyValueDateTime。
这甚至可以使用实体框架吗?