{"record":{"id":"0bc1c6ff7cfd0788","repo":"dotnet/efcore","slug":"an-azure-cosmos-db-container-name-is-defined-on-en","errorCode":null,"errorMessage":"An Azure Cosmos DB container name is defined on entity type '{entityType}', which inherits from '{baseEntityType}'. Container names must be defined on the root entity type of a hierarchy.","messagePattern":"An Azure Cosmos DB container name is defined on entity type '(.+?)', which inherits from '(.+?)'\\. Container names must be defined on the root entity type of a hierarchy\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":92,"sourceCode":"    /// </summary>\n    protected virtual void ValidateSharedContainerCompatibility(\n        IModel model,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        // All entity types mapped to a single container must have the same container-level settings, most notably partition keys.\n        var containers = new Dictionary<string, List<IEntityType>>();\n        foreach (var entityType in model.GetEntityTypes().Where(et => et.FindPrimaryKey() != null))\n        {\n            var container = entityType.GetContainer();\n            if (container == null)\n            {\n                continue;\n            }\n\n            if (entityType.BaseType != null\n                && 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(),","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L74-L110","documentation":"In a TPH hierarchy that shares one Cosmos container, the container name may only be set on the root entity type. CosmosModelValidator throws InvalidOperationException via CosmosStrings.ContainerNotOnRoot when a derived entity type that has a BaseType also carries a container-name annotation, because all documents in a hierarchy must live in the root's container.","triggerScenarios":"Calling ToContainer/HasContainerName on a derived entity type that participates in an inheritance hierarchy (has a BaseType).","commonSituations":"Applying a per-type container convention that fires for derived types; copy-pasting ToContainer across an inheritance chain; migrating from a per-type mapping scheme.","solutions":["Set the container name only on the base/root entity type and remove the ToContainer call from derived types.","If types truly need separate containers, they should not be in the same inheritance hierarchy (break the hierarchy)."],"exampleFix":"// before (container set on derived type)\nmodelBuilder.Entity<Animal>().ToContainer(\"animals\");\nmodelBuilder.Entity<Cat>().ToContainer(\"cats\"); // Cat derives from Animal\n\n// after (container only on the root)\nmodelBuilder.Entity<Animal>().ToContainer(\"animals\");\n// Cat inherits the container from Animal","handlingStrategy":"validation","validationCode":"// Ensure ToContainer is only set on hierarchy roots.\nforeach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    if (et.BaseType != null\n        && et.FindAnnotation(CosmosAnnotationNames.ContainerName)?.Value != null)\n    {\n        throw new InvalidOperationException($\"Remove ToContainer from derived type {et.Name}.\");\n    }\n}","typeGuard":"static bool IsHierarchyRoot(IEntityType et) => et.BaseType is null;","tryCatchPattern":"try { context.Database.EnsureCreated(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"root entity type\"))\n{ throw new InvalidOperationException(\"Set container names only on the base/root entity type.\", ex); }","preventionTips":["Set ToContainer only on the root entity type of an inheritance hierarchy.","Make container conventions skip derived types (BaseType != null).","If types need separate containers, break the inheritance relationship."],"tags":["cosmos","inheritance","container","model-validation","tph"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}