我有复杂的审计字段类型
我的复杂类型:
[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; }
}
我的基本实体(所有其他继承者)具有 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 在 fluent API 中有解决方案吗?
最佳答案
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.
由此可见,复杂类型不能参与实体之间的任何关系,因此它们不能包含外键
关于ef-code-first - EF-Code First 复杂类型的导航属性外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33065466/