我们收到以下错误,该错误似乎仅在将日期时间添加到值对象时才会发生。'实体类型'TimeWindow'无法配置为拥有的,因为它已被配置为非拥有的。如果您想覆盖以前的配置,请首先通过调用“忽略”从模型中删除实体类型。
值对象类:
public class TimeWindow : ValueObject
{
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
private TimeWindow()
{
}
public TimeWindow(
DateTime? startTime,
DateTime? endTime)
{
StartTime = startTime;
EndTime = endTime;
}
protected override IEnumerable<object> GetAtomicValues()
{
yield return StartTime;
yield return EndTime;
}
}
Run Code Online (Sandbox Code Playgroud)
在 OnModelCreating 内部,我们添加了 OwnsOne 关系:
builder.Entity<Manifest>(b =>
{
b.ToTable(DistributionConsts.DbTablePrefix + "Manifests", DistributionConsts.DbSchema);
b.ConfigureByConvention();
b.OwnsOne(b => b.TimeWindow);
});
Run Code Online (Sandbox Code Playgroud)
我们将 TimeWindow 值对象添加到的实体:
public class Manifest : FullAuditedAggregateRoot<Guid>
{
protected …Run Code Online (Sandbox Code Playgroud)