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).
CodePudding user response:
builder.Ignore<TimeWindow>();
builder.Entity<Manifest>(b =>
{
b.ToTable(DistributionConsts.DbTablePrefix "Manifests", DistributionConsts.DbSchema);
b.ConfigureByConvention();
b.OwnsOne(b => b.TimeWindow);
});
Adding builder.Ignore<TimeWindow>();
line will remove the entity type from the model and allowed me to override it and configure it as OwnsOne