dotnet/efcore · error · InvalidOperationException

The entity type '{entityType}' is owned by the entity type '

Error message

The entity type '{entityType}' is owned by the entity type '{owner}', but is mapped to the container '{container}'. Owned types mapped to a container directly are not supported, remove this configuration to allow the owned type to be embedded in the same document as the owner.

What it means

Owned types are embedded inside the owner's JSON document in Cosmos; they cannot be mapped to their own container. CosmosModelValidator throws InvalidOperationException via CosmosStrings.OwnedTypeDifferentContainer when an entity type that has an ownership also has a container name configured.

Source

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

        foreach (var entityType in model.GetEntityTypes().Where(et => et.FindPrimaryKey() != null))
        {
            var container = entityType.GetContainer();
            if (container == null)
            {
                continue;
            }

            if (entityType.BaseType != null
                && 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 = [];

View on GitHub (pinned to dbf9771522)

Solutions

  1. Remove the ToContainer/HasContainerName configuration from the owned type so it embeds in the owner's document.
  2. If the type must live in its own container, remove the ownership (make it a regular relationship) instead.

Example fix

// before (owned type also given its own container)
modelBuilder.Entity<Order>(o =>
{
    o.OwnsOne(p => p.ShippingAddress, sa => sa.ToContainer("addresses"));
});

// after (owned type embeds in the owner document)
modelBuilder.Entity<Order>(o =>
{
    o.OwnsOne(p => p.ShippingAddress);
});
Defensive patterns

Strategy: validation

Validate before calling

// Owned types must not carry a container name.
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
    if (et.FindOwnership() != null && et.GetContainer() != null)
        throw new InvalidOperationException($"Remove ToContainer from owned type {et.Name}.");
}

Type guard

static bool IsOwnedType(IEntityType et) => et.FindOwnership() != null;

Try / catch

try { context.Database.EnsureCreated(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Owned types mapped to a container"))
{ throw new InvalidOperationException("Remove ToContainer from owned types; they embed in the owner.", ex); }

Prevention

When it happens

Trigger: Calling OwnsOne/OwnsMany on an entity AND calling ToContainer/HasContainerName on that same owned entity type (directly or via a convention).

Common situations: A global convention that sets a container on every entity type including owned ones; configuring an owned type as if it were an aggregate root; mixing own-and-container after refactoring.

Related errors


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