EF-Code First navigation 属性 复杂类型的外键

EF-Code First navigation property foreign key in complex type

我的审计字段类型很复杂

我的复杂类型:

[ComplexType]
public class AuditData  {
    [Column("CreatorUserId")]
    public int? CreatorUserId { get; set; }
    public DateTime? CreationTime { get; set; }
    [Column("ModifierUserId")]
    public int? ModifierUserId { get; set; }
    public DateTime? ModificationTime { get; set; }
}

我的基本实体(所有其他 inherti 这个)有 AuditData 属性:

public abstract class Entity : IEntity, IAuditedEntity, INotifiedDbContextBeforeSave
{

    // Summary:
    //     Unique identifier for this entity.
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public int Old_Id { get; set; }
    public string Old_TableName { get; set; }        
    [Timestamp]
    public byte[] RowVersion { get; set; }        
    public AuditData AuditData { get; set; }
    // can this 2 lines below work as navigation property with foreign key in complex type
    public virtual User CreatorUser { get; set; }
    public virtual User ModifierUser { get; set; }

    //... other fields
}

我有 2 个导航属性 CreatorUser 和 ModifierUser。 我知道你不能在 ComplexType 中使用导航 属性,但是我在实体上的导航 属性 可以用 complexType

中的外键映射吗

类似于:

    [ForeignKey("CreatorUserId")] // --> should point to AuditData.CreatorUserId
    public virtual User CreatorUser { get; set; }

因为 CreatorUserId 在每个实体中都是 属性 但 EF 不知道它。 Mybe 有流利的解决方案 API ?

official documentation 说:

Complex types are non-scalar properties of entity types that enable scalar properties to be organized within entities. Like entities, complex types consist of scalar properties or other complex type properties. Because complex types do not have keys, complex type objects cannot be managed by the Entity Framework apart from the parent object.

由此可见,复杂类型不能参与实体之间的任何关系,因此它们不能包含外键