dotnet/efcore · error · InvalidOperationException

The entity type '{entityType}' is mapped to the container '{

Error message

The entity type '{entityType}' is mapped to the container '{container}' but it is also configured as being contained in property '{property}'.

What it means

An entity type cannot be both mapped to its own container and embedded as a contained JSON property of another entity. CosmosModelValidator throws InvalidOperationException via CosmosStrings.ContainerContainingPropertyConflict when an entity has a container name AND a containing-property name (GetContainingPropertyName) set.

Source

Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:108

                && entityType.FindAnnotation(CosmosAnnotationNames.ContainerName)?.Value != null)
            {
                throw new InvalidOperationException(
                    CosmosStrings.ContainerNotOnRoot(entityType.DisplayName(), entityType.BaseType.DisplayName()));
            }

            var ownership = entityType.FindOwnership();
            if (ownership != null)
            {
                throw new InvalidOperationException(
                    CosmosStrings.OwnedTypeDifferentContainer(
                        entityType.DisplayName(),
                        ownership.PrincipalEntityType.DisplayName(),
                        container));
            }

            if (entityType.GetContainingPropertyName() != null)
            {
                throw new InvalidOperationException(
                    CosmosStrings.ContainerContainingPropertyConflict(
                        entityType.DisplayName(),
                        container,
                        entityType.GetContainingPropertyName()));
            }

            if (!containers.TryGetValue(container, out var mappedTypes))
            {
                mappedTypes = [];
                containers[container] = mappedTypes;
            }

            mappedTypes.Add(entityType);
        }

        foreach (var (container, mappedTypes) in containers)
        {
            ValidateSharedContainerCompatibility(mappedTypes, container, logger);

View on GitHub (pinned to dbf9771522)

Solutions

  1. Choose one mapping: either map the entity to its own container (remove the ownership/containing-property configuration) or embed it (remove ToContainer).
  2. Audit conventions that set container names so they skip types that have a containing property.

Example fix

// before (both container and contained-in-property)
modelBuilder.Entity<Address>().ToContainer("addresses");
modelBuilder.Entity<Customer>(c =>
{
    c.OwnsOne(x => x.Address); // embeds Address as a JSON property
});

// after (embed only)
modelBuilder.Entity<Customer>(c =>
{
    c.OwnsOne(x => x.Address);
});
// ToContainer on Address removed
Defensive patterns

Strategy: validation

Validate before calling

// An entity cannot be both container-mapped and contained-in-a-property.
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
    if (et.GetContainer() != null && et.GetContainingPropertyName() != null)
        throw new InvalidOperationException($"{et.Name} is both container-mapped and embedded; pick one.");
}

Try / catch

try { context.Database.EnsureCreated(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("contained in property"))
{ throw new InvalidOperationException("Choose container OR embedding for this entity, not both.", ex); }

Prevention

When it happens

Trigger: Configuring ToContainer on an entity that is also referenced through a navigation that embeds it as a JSON property (owned/contained), producing both a container and a containing-property annotation.

Common situations: A convention that assigns containers globally colliding with a containing-property mapping; refactoring an owned type into a container-backed type without removing the containment config.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/bb5cde13d46a2605. Report an issue: GitHub.