{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L90-L126","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Choose one mapping: either map the entity to its own container (remove the ownership/containing-property configuration) or embed it (remove ToContainer).","Audit conventions that set container names so they skip types that have a containing property."],"exampleFix":"// before (both container and contained-in-property)\nmodelBuilder.Entity<Address>().ToContainer(\"addresses\");\nmodelBuilder.Entity<Customer>(c =>\n{\n    c.OwnsOne(x => x.Address); // embeds Address as a JSON property\n});\n\n// after (embed only)\nmodelBuilder.Entity<Customer>(c =>\n{\n    c.OwnsOne(x => x.Address);\n});\n// ToContainer on Address removed","handlingStrategy":"validation","validationCode":"// An entity cannot be both container-mapped and contained-in-a-property.\nforeach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    if (et.GetContainer() != null && et.GetContainingPropertyName() != null)\n        throw new InvalidOperationException($\"{et.Name} is both container-mapped and embedded; pick one.\");\n}","typeGuard":null,"tryCatchPattern":"try { context.Database.EnsureCreated(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"contained in property\"))\n{ throw new InvalidOperationException(\"Choose container OR embedding for this entity, not both.\", ex); }","preventionTips":["Decide per type: container-backed root OR embedded (owned/contained) property, never both.","Audit container conventions so they skip types with a containing-property name.","When refactoring an owned type into a container-backed type, remove the containment config."],"tags":["cosmos","container","embedding","owned-types","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}