{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L74-L110","documentation":"CosmosModelValidator.ValidateSharedContainerCompatibility throws InvalidOperationException(CosmosStrings.ContainerNotOnRoot) when a derived entity type (entityType.BaseType != null) has its own ContainerName annotation. Cosmos requires the container to be declared only on the root entity type of an inheritance hierarchy; setting it on a derived type is ambiguous because the discriminator and container must be consistent across the hierarchy.","triggerScenarios":"Calling HasContainerName(\"X\") on a derived entity in a TPH hierarchy; configuring [Container(\"X\")] on a subclass; setting ToContainer on a derived type while the base type has its own container.","commonSituations":"Migrating from per-type configuration to TPH and forgetting to remove the container name on derived types; using ApplyConfigurationsFromAssembly where an IEntityTypeConfiguration<T> for a derived type sets the container; conventions that copy container names down an inheritance chain.","solutions":["Set HasContainerName on the root entity type of the hierarchy only; remove the configuration from derived types.","If derived types need different containers, they cannot share an inheritance hierarchy with the base type - split them into separate roots.","Audit IEntityTypeConfiguration<T> classes for any ToContainer/HasContainerName calls on derived types and remove them.","If configuration is shared via a common base configuration, branch on whether entityType.BaseType is null before applying the container name."],"exampleFix":"// before - throws: derived type sets its own container\nclass AnimalConfig : IEntityTypeConfiguration<Animal> { /* sets Container=Animals */ }\nclass DogConfig : IEntityTypeConfiguration<Dog>\n{\n    public void Configure(EntityTypeBuilder<Dog> b)\n        => b.HasContainerName(\"Dogs\"); // Dog derives from Animal\n}\n\n// after - only the root sets the container\nclass DogConfig : IEntityTypeConfiguration<Dog>\n{\n    public void Configure(EntityTypeBuilder<Dog> b) { /* no HasContainerName */ }\n}","handlingStrategy":"validation","validationCode":"// Only set container name on root entity types.\nstatic void SetContainer<T>(EntityTypeBuilder<T> b, string name) where T : class\n{\n    if (b.Metadata.BaseType is not null)\n        throw new InvalidOperationException($\"Set container on {b.Metadata.BaseType.DisplayName()} instead.\");\n    b.HasContainerName(name);\n}","typeGuard":"static bool IsRootEntityType(IEntityType et) => et.BaseType is null;","tryCatchPattern":null,"preventionTips":["Set HasContainerName on the root entity of a hierarchy only.","Audit IEntityTypeConfiguration<T> classes for ToContainer/HasContainerName on derived types.","Do not copy container configuration down an inheritance chain in shared conventions.","Validate the model at startup so misconfiguration surfaces early."],"tags":["ef-core","cosmos","model-validation","inheritance","container"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}