{"record":{"id":"bb5cde13d46a2605","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-is-mapped-to-the-co","errorCode":null,"errorMessage":"The entity type '{entityType}' is mapped to the container '{container}' but it is also configured as being contained in property '{property}'.","messagePattern":"The entity type '(.+?)' is mapped to the container '(.+?)' but it is also configured as being contained in property '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":108,"sourceCode":"                && entityType.FindAnnotation(CosmosAnnotationNames.ContainerName)?.Value != null)\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.ContainerNotOnRoot(entityType.DisplayName(), entityType.BaseType.DisplayName()));\n            }\n\n            var ownership = entityType.FindOwnership();\n            if (ownership != null)\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.OwnedTypeDifferentContainer(\n                        entityType.DisplayName(),\n                        ownership.PrincipalEntityType.DisplayName(),\n                        container));\n            }\n\n            if (entityType.GetContainingPropertyName() != null)\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.ContainerContainingPropertyConflict(\n                        entityType.DisplayName(),\n                        container,\n                        entityType.GetContainingPropertyName()));\n            }\n\n            if (!containers.TryGetValue(container, out var mappedTypes))\n            {\n                mappedTypes = [];\n                containers[container] = mappedTypes;\n            }\n\n            mappedTypes.Add(entityType);\n        }\n\n        foreach (var (container, mappedTypes) in containers)\n        {\n            ValidateSharedContainerCompatibility(mappedTypes, container, logger);","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L90-L126","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before - throws: entity is both embedded (via OwnsOne) and given a container\nentity.OwnsOne(e => e.Detail);\nmodelBuilder.Entity<Detail>().HasContainerName(\"Details\");\n// (Detail ends up with a containing property name AND a container)\n\n// after - Detail is its own document root only\nmodelBuilder.Entity<Detail>().HasContainerName(\"Details\");\nentity.HasOne(e => e.Detail)\n    .WithOne()\n    .HasForeignKey<Detail>(d => d.OwnerId);","handlingStrategy":"validation","validationCode":"// Detect the conflict before validation runs.\nstatic void AssertNotConflicting(IEntityType et)\n{\n    if (et.GetContainer() is not null && et.GetContainingPropertyName() is not null)\n        throw new InvalidOperationException($\"{et.DisplayName()} is both embedded and has a container; pick one.\");\n}","typeGuard":"static bool IsEmbedded(IEntityType et) => et.GetContainingPropertyName() is not null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["ef-core","cosmos","model-validation","owned-types","container"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}