dotnet/efcore · error · InvalidOperationException
The entity type '{entityType}' is sharing the container '{co
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
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.
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 dbf9771522)
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.
Example fix
// before (Dog has no discriminator value)
modelBuilder.Entity<Cat>(b => { b.ToContainer("pets"); b.HasDiscriminator(c => c.Kind).HasValue("cat"); });
modelBuilder.Entity<Dog>(b => { b.ToContainer("pets"); b.HasDiscriminator(d => d.Kind); });
// after (value assigned)
modelBuilder.Entity<Dog>(b => { b.ToContainer("pets"); b.HasDiscriminator(d => d.Kind).HasValue("dog"); }); Defensive patterns
Strategy: validation
Validate before calling
// Every instantiable type in a shared container needs a discriminator value.
var byContainer = modelBuilder.Model.GetEntityTypes()
.Where(e => e.FindPrimaryKey() != null && e.GetContainer() != null && e.ClrType.IsInstantiable())
.GroupBy(e => e.GetContainer());
foreach (var g in byContainer.Where(g => g.Count() > 1))
foreach (var et in g.Where(e => e.GetDiscriminatorValue() == null))
throw new InvalidOperationException($"{et.Name} needs a discriminator value."); Try / catch
try { context.Database.EnsureCreated(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("discriminator value"))
{ throw new InvalidOperationException("Assign a HasValue(...) discriminator to each type.", ex); } Prevention
- 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.
When it happens
Trigger: Configuring HasDiscriminator but omitting .HasValue(...) for one of the concrete entity types in a shared container.
Common situations: 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.
Related errors
- The entity type '{entityType}' is sharing the container '{co
- The discriminator value for '{entityType1}' is '{discriminat
- The IsDiscriminatorMappingComplete setting was configured to
- The partition key properties for entity type '{entityType1}'
- The partition key property '{property1}' on '{entityType1}'
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/9c65571506323535.
Report an issue: GitHub.