如何在 Fluent 中使用 ComplexType - CodeFirst

how to use ComplexType in Fluent - CodeFirst

在我的项目中,我想为每个 Table 记录一些事件,例如以下事件:

CreatedOn, CreatdBy,IsDeleted,DeletedBy,DeletedOn

为此,我创建了一个 ComplexType,如下所示:

 [ComplexType]
public class Log
{
    public DateTime CreatedOn { get; set; }
    public User CreatedBy { get; set; }
    public Guid CreatorId { get; set; }
    public bool IsDeleted { get; set; }
    public User DeletedBy { get; set; }
    public Guid? DeletedUserId { get; set; }

}

}

并像这样在另一个 Table 中使用 :

 public class StoreMaster
{
//some fields
public virtual Log Log { get; set; }
}

我的流利程度是这样的:

 HasRequired(row => row.Log.CreatedBy).WithMany(row => row.StoreMasters).HasForeignKey(row => row.Log.CreatorId).WillCascadeOnDelete();

当 运行 Project I 收到此错误时:

The expression 'row => row.Log.CreatedBy' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'.

恐怕你想要达到的目标是不可能的。首先是复杂类型 只能包含基元属性 ,因此您不能在那里声明导航属性。第二件事是您在复杂类型中定义的标量属性不能用于 relationship.The 父对象的定义中,您将复杂类型声明为 属性 是唯一可以管理它的人。