{"record":{"id":"9c65571506323535","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-is-sharing-the-cont-9c6557","errorCode":null,"errorMessage":"The entity type '{entityType}' is sharing the container '{container}' with other types, but does not have a discriminator value configured. Configure a unique discriminator value for this entity type.","messagePattern":"The entity type '(.+?)' is sharing the container '(.+?)' with other types, but does not have a discriminator value configured\\. Configure a unique discriminator value for this entity type\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":207,"sourceCode":"            }\n\n            if (mappedTypes.Count == 1)\n            {\n                break;\n            }\n\n            if (entityType.ClrType.IsInstantiable()\n                && entityType.GetContainingPropertyName() == null)\n            {\n                if (entityType.FindDiscriminatorProperty() == null)\n                {\n                    throw new InvalidOperationException(CosmosStrings.NoDiscriminatorProperty(entityType.DisplayName(), container));\n                }\n\n                var discriminatorValue = entityType.GetDiscriminatorValue();\n                if (discriminatorValue == null)\n                {\n                    throw new InvalidOperationException(CosmosStrings.NoDiscriminatorValue(entityType.DisplayName(), container));\n                }\n\n                if (discriminatorValues.TryGetValue(discriminatorValue, out var duplicateEntityType))\n                {\n                    throw new InvalidOperationException(\n                        CosmosStrings.DuplicateDiscriminatorValue(\n                            entityType.DisplayName(),\n                            discriminatorValue,\n                            duplicateEntityType.DisplayName(),\n                            container));\n                }\n\n                discriminatorValues[discriminatorValue] = entityType;\n\n                var currentIsDiscriminatorMappingComplete = entityType.GetIsDiscriminatorMappingComplete();\n                if (isDiscriminatorMappingComplete == null)\n                {\n                    isDiscriminatorMappingComplete = currentIsDiscriminatorMappingComplete;","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L189-L225","documentation":"A discriminator property exists but no unique discriminator value is assigned for an entity type sharing a container. CosmosModelValidator throws InvalidOperationException via CosmosStrings.NoDiscriminatorValue when GetDiscriminatorValue() returns null for an instantiable type in a shared container.","triggerScenarios":"Configuring HasDiscriminator but omitting .HasValue(...) for one of the concrete entity types in a shared container.","commonSituations":"Adding a new derived/root type and forgetting its discriminator value; relying on convention that did not fill the value; abstract base configured but concrete types missing values.","solutions":["Assign a unique discriminator value via .HasValue(...) for each concrete entity type sharing the container.","Verify GetDiscriminatorValue() is non-null for every instantiable type in the shared container."],"exampleFix":"// before (Dog has no discriminator value)\nmodelBuilder.Entity<Cat>(b => { b.ToContainer(\"pets\"); b.HasDiscriminator(c => c.Kind).HasValue(\"cat\"); });\nmodelBuilder.Entity<Dog>(b => { b.ToContainer(\"pets\"); b.HasDiscriminator(d => d.Kind); });\n\n// after (value assigned)\nmodelBuilder.Entity<Dog>(b => { b.ToContainer(\"pets\"); b.HasDiscriminator(d => d.Kind).HasValue(\"dog\"); });","handlingStrategy":"validation","validationCode":"// Every instantiable type in a shared container needs a discriminator value.\nvar byContainer = modelBuilder.Model.GetEntityTypes()\n    .Where(e => e.FindPrimaryKey() != null && e.GetContainer() != null && e.ClrType.IsInstantiable())\n    .GroupBy(e => e.GetContainer());\nforeach (var g in byContainer.Where(g => g.Count() > 1))\nforeach (var et in g.Where(e => e.GetDiscriminatorValue() == null))\n    throw new InvalidOperationException($\"{et.Name} needs a discriminator value.\");","typeGuard":null,"tryCatchPattern":"try { context.Database.EnsureCreated(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"discriminator value\"))\n{ throw new InvalidOperationException(\"Assign a HasValue(...) discriminator to each type.\", ex); }","preventionTips":["After HasDiscriminator, always set HasValue on each concrete type.","Add a build-time test asserting GetDiscriminatorValue() is non-null per shared container.","When adding a new type, set its discriminator value immediately."],"tags":["cosmos","discriminator","shared-container","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}