dotnet/efcore · error · InvalidOperationException
The entity type ' ' is mapped to the container ' ' but it…
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
CosmosModelValidator.ValidateSharedContainerCompatibility throws InvalidOperationException(CosmosStrings.ContainerContainingPropertyConflict) when an entity type has both a ContainerName annotation AND a containing property name (entityType.GetContainingPropertyName() != null). A containing property name means the entity is embedded as a property of another entity (e.g. an owned type that is also keyed), so mapping it to its own container at the same time is contradictory.
Solutions
- Decide whether the entity is embedded (has a containing property) or a document root mapped to a container - it cannot be both. Remove either ToContainer or the embedding (OwnsOne/Keyed) configuration.
- If you intended the entity to be its own document, remove the OwnsOne/Keyed mapping that created the containing property name.
- If you intended it to be embedded, remove HasContainerName/ToContainer.
- Audit shared configuration code so it does not blanket-apply ToContainer to entities that are also keyed or owned.
Example fix
// before - throws: entity is both embedded (via OwnsOne) and given a container
entity.OwnsOne(e => e.Detail);
modelBuilder.Entity<Detail>().HasContainerName("Details");
// (Detail ends up with a containing property name AND a container)
// after - Detail is its own document root only
modelBuilder.Entity<Detail>().HasContainerName("Details");
entity.HasOne(e => e.Detail)
.WithOne()
.HasForeignKey<Detail>(d => d.OwnerId); Defensive patterns
Strategy: validation
Validate before calling
// Detect the conflict before validation runs.
static void AssertNotConflicting(IEntityType et)
{
if (et.GetContainer() is not null && et.GetContainingPropertyName() is not null)
throw new InvalidOperationException($"{et.DisplayName()} is both embedded and has a container; pick one.");
} Type guard
static bool IsEmbedded(IEntityType et) => et.GetContainingPropertyName() is not null;
Prevention
- Decide: a type is either embedded (owned/keyed) or a document root with a container - not both.
- When adopting keyed entities, audit ToContainer calls that may now conflict.
- Skip embedded types when applying container configuration generically.
- Validate the model at startup so the conflict surfaces before the first SaveChanges.
When it happens
Trigger: Configuring an entity with HasContainerName while it is also referenced as an embedded/contained property via OwnsOne or complex types; an entity that is both owned (embedded) and given its own container mapping; mixing Keyed types (which carry a containing property name) with ToContainer.
Common situations: Adopting the new keyed-entity feature and accidentally applying ToContainer on a keyed entity; configuration shared between entity types where one is embedded; complex type configuration that also sets container.
Related errors
- The entity type ' ' is owned by the entity type ' ', but is…
- An Azure Cosmos DB container name is defined on entity type
- Entity type ' ' has a split mapping for ' ' that is shared…
- Entity type ' ' is an optional dependent using table…
- The discriminator value for
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/bb5cde13d46a2605.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)