【问题标题】:EF-Code First navigation property foreign key in complex typeEF-Code First 复杂类型的导航属性外键
【发布时间】:2016-01-09 00:11:24
【问题描述】:

我有复杂的审计字段类型

我的复杂类型:

[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 中有解决方案吗?

【问题讨论】:

    标签: ef-code-first entity-framework-6


    【解决方案1】:

    official documentation 说:

    复杂类型是实体类型的非标量属性,可以在实体内组织标量属性。与实体一样,复杂类型由标量属性或其他复杂类型属性组成。因为复杂类型没有键,所以实体框架除了父对象外,无法管理复杂类型对象。

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

    【讨论】:

      猜你喜欢
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 2016-03-16
      • 1970-01-01
      相关资源
      最近更新 更多