We are getting the following error, which only seems to occur when datetimes are added to the value object. 'The entity type 'TimeWindow' cannot be configured as owned because it has already been configured as a non-owned. If you want to override previous configuration first remove the entity type from the model by calling 'Ignore'.
The Value object class:
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;
}
}
Inside of OnModelCreating we've added an OwnsOne relationship:
builder.Entity<Manifest>(b =>
{
b.ToTable(DistributionConsts.DbTablePrefix + "Manifests", DistributionConsts.DbSchema);
b.ConfigureByConvention();
b.OwnsOne(b => b.TimeWindow);
});
The Entity that we are adding the TimeWindow value object to:
public class Manifest : FullAuditedAggregateRoot<Guid>
{
protected Manifest()
{
}
public Manifest(
Guid id) : base(id)
{
}
public virtual TimeWindow TimeWindow { get; set; }
}
We have another entity with a different ValueObject configured the same way, but without any DateTimes and we haven't received any errors.
Adding .Ignore(x => x.TimeWindow);
before the builder and inside the builder still errors (as suggested by the error).
TimeWindow
class as entity type (see Including types in the model in EF Core docs)) before theb.OwnsOne(b => b.TimeWindow);
call. Find and eliminate/fix that place. That's all I can say. – Expectation