Adding Value Object to EF Entity - The entity type cannot be configured as owned because it has already been configured as a non-owned
Asked Answered
L

2

8

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

Laski answered 22/3, 2022 at 13:44 Comment(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 in EF Core docs)) before the b.OwnsOne(b => b.TimeWindow); call. Find and eliminate/fix that place. That's all I can say.Expectation
L
7
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

Laski answered 22/3, 2022 at 16:45 Comment(0)
B
1

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
Brelje answered 8/8, 2022 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.