我有一个拥有类型的实体,我想与另一个实体建立关系,但外部属性示例上存在外键属性: - 这是我的员工实体
public sealed class Employee : AuditedAggregateRoot
{
public WorkInformation WorkInformation { get; private set; }
}
它包含一个名为WorkInformation
的值Object(Owned Type)public class WorkInformation : ValueObject<WorkInformation>
{
private WorkInformation()
{
}
public int? DepartmentId { get; private set; }
}
我需要在Employee和Department之间建立关系
public class Department : AuditedAggregateRoot
{
}
我使用以下Fluent配置来做到这一点,但我收到了错误
builder.OwnsOne(e => e.WorkInformation)
//Add Employee Relations
builder.HasOne<Department>()
.WithMany()
.IsRequired(false)
.HasForeignKey(e => e.WorkInformation.DepartmentId);
如果我将DepartmentId移动到所有者实体,它可以正常工作。
答案 0 :(得分:3)
无法通过所有者类型构建器配置所拥有的类型(其属性,关系等)。相反,请使用ReferenceOwnershipBuilder
方法返回的OwnsOne
:
var workInfomationBuilder = builder.OwnsOne(e => e.WorkInformation);
//Add Employee Relations
workInfomationBuilder.HasOne<Department>()
.WithMany()
.IsRequired(false)
.HasForeignKey(e => e.DepartmentId);