Ef code first EF代码复杂类型中的第一个导航属性外键
我有审计字段的复杂类型 我的复杂类型:Ef code first EF代码复杂类型中的第一个导航属性外键,ef-code-first,entity-framework-6,Ef Code First,Entity Framework 6,我有审计字段的复杂类型 我的复杂类型: [ComplexType] public class AuditData { [Column("CreatorUserId")] public int? CreatorUserId { get; set; } public DateTime? CreationTime { get; set; } [Column("ModifierUserId")] public int? ModifierUserId { get;
[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
}
我有两个导航属性CreatorUser和ModifierUser。
我知道您不能在ComplexType中具有导航属性,但可以在ComplexType中使用外键映射实体上的导航属性吗
比如:
[ForeignKey("CreatorUserId")] // --> should point to AuditData.CreatorUserId
public virtual User CreatorUser { get; set; }
因为CreatorUserId将是每个实体的属性,但EF不知道它。
Mybe fluent API中是否有解决方案?说明:
复杂类型是实体类型的非标量属性,可以在实体中组织标量属性。与实体一样,复杂类型由标量属性或其他复杂类型属性组成。因为复杂类型没有键,所以除了父对象之外,实体框架无法管理复杂类型对象
因此,复杂类型不能参与实体之间的任何关系,因此它们不能包含外键