4

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).

Jame
  • 131
  • 2
  • 7
  • 1
    I can speak only for EF Core. For me, apparently some code not shown in the post (either yours or ABP or whatever framework/library) is registering your `TimeWindow` class as **entity type** (see [Including types in the model](https://learn.microsoft.com/en-us/ef/core/modeling/entity-types?tabs=data-annotations#including-types-in-the-model) in EF Core docs)) **before** the `b.OwnsOne(b => b.TimeWindow);` call. Find and eliminate/fix that place. That's all I can say. – Ivan Stoev Mar 22 '22 at 16:24

2 Answers2

4
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

Jame
  • 131
  • 2
  • 7
0

Decorating owned entity with [Owned] also solves the problem, like

using Microsoft.EntityFrameworkCore;

[Owned]
public class TimeWindow : ValueObject

Not related but same error: This error can also be shown when u have the Id of the owned entity not set to long.

 public class OwnedEntity : AuditedEntity<Guid> //error
 public class OwnedEntity : AuditedEntity<long> //correct
Siphamandla Ngwenya
  • 2,696
  • 1
  • 16
  • 21