dotnet/efcore · error · InvalidOperationException
The entity type ' ' is sharing the container ' ' with other…
Error message
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. What it means
CosmosModelValidator.ValidateSharedContainerCompatibility throws InvalidOperationException(CosmosStrings.NoDiscriminatorValue) when an instantiable entity type sharing a container has a discriminator property configured but no discriminator value assigned (entityType.GetDiscriminatorValue() == null). EF needs a concrete value to write into the discriminator field for each document so it can route queries to the correct type.
Solutions
- Add HasValue(...) right after HasDiscriminator for every instantiable type sharing the container.
- If using attributes, make sure each type carries a [Discriminator] value (or whatever attribute the configuration reads).
- If the type is meant to be abstract, ensure ClrType is abstract so IsInstantiable() returns false and skips this check.
- Run model validation at startup to surface missing values before the first SaveChanges.
Example fix
// before - throws: discriminator property set, no value
modelBuilder.Entity<A>()
.ToContainer("Shared")
.HasDiscriminator(a => a.Kind);
// after - assign a value
modelBuilder.Entity<A>()
.ToContainer("Shared")
.HasDiscriminator(a => a.Kind).HasValue("a"); Defensive patterns
Strategy: validation
Validate before calling
// Pre-validate: every instantiable shared-container type with a discriminator must also have a value.
static bool AllHaveDiscriminatorValues(IReadOnlyList<IEntityType> types)
=> types.Where(t => t.ClrType.IsInstantiable() && t.GetContainingPropertyName() is null)
.All(t => t.GetDiscriminatorValue() is not null); Prevention
- Always chain HasValue(...) after HasDiscriminator(...).
- Make abstract types abstract in CLR so IsInstantiable() returns false and skips the check.
- Audit IEntityTypeConfiguration<T> classes to ensure HasValue is present after HasDiscriminator.
- Validate the model at startup to surface missing values before SaveChanges.
When it happens
Trigger: Calling HasDiscriminator(x => x.Kind) without a following HasValue(...); configuring a discriminator property via attribute or convention but never setting the value; abstract base types where the default convention would skip the value but the type became instantiable.
Common situations: Forgetting HasValue after HasDiscriminator; switching from default-discriminator conventions to explicit configuration and missing the value; discriminator value lost during a refactor.
Related errors
- The discriminator value for
- The entity type ' ' is sharing the container ' ' with other…
- The partition key properties for entity type
- The partition key property
- An Azure Cosmos DB container name is defined on entity type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/9c65571506323535.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:207
}
if (mappedTypes.Count == 1)
{
break;
}
if (entityType.ClrType.IsInstantiable()
&& entityType.GetContainingPropertyName() == null)
{
if (entityType.FindDiscriminatorProperty() == null)
{
throw new InvalidOperationException(CosmosStrings.NoDiscriminatorProperty(entityType.DisplayName(), container));
}
var discriminatorValue = entityType.GetDiscriminatorValue();
if (discriminatorValue == null)
{
throw new InvalidOperationException(CosmosStrings.NoDiscriminatorValue(entityType.DisplayName(), container));
}
if (discriminatorValues.TryGetValue(discriminatorValue, out var duplicateEntityType))
{
throw new InvalidOperationException(
CosmosStrings.DuplicateDiscriminatorValue(
entityType.DisplayName(),
discriminatorValue,
duplicateEntityType.DisplayName(),
container));
}
discriminatorValues[discriminatorValue] = entityType;
var currentIsDiscriminatorMappingComplete = entityType.GetIsDiscriminatorMappingComplete();
if (isDiscriminatorMappingComplete == null)
{
isDiscriminatorMappingComplete = currentIsDiscriminatorMappingComplete;View on GitHub (pinned to 3a2006ef56)