dotnet/efcore · error · InvalidOperationException
The IsDiscriminatorMappingComplete setting was configured to
Error message
The IsDiscriminatorMappingComplete setting was configured to '{isDiscriminatorMappingComplete1}' on '{entityType1}', but on '{entityType2}' it was configured to '{isDiscriminatorMappingComplete2}'. All entity types mapped to the same container '{container}' must be configured with the same 'IsDiscriminatorMappingComplete' value. What it means
The IsDiscriminatorMappingComplete setting (whether the discriminator mapping is considered complete/abstract) must be consistent across all entity types sharing a container. CosmosModelValidator throws InvalidOperationException via CosmosStrings.IsDiscriminatorMappingCompleteMismatch when one type is configured complete and another is not, because a single container needs one coherent discriminator policy.
Source
Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:229
{
throw new InvalidOperationException(
CosmosStrings.DuplicateDiscriminatorValue(
entityType.DisplayName(),
discriminatorValue,
duplicateEntityType.DisplayName(),
container));
}
discriminatorValues[discriminatorValue] = entityType;
var currentIsDiscriminatorMappingComplete = entityType.GetIsDiscriminatorMappingComplete();
if (isDiscriminatorMappingComplete == null)
{
isDiscriminatorMappingComplete = currentIsDiscriminatorMappingComplete;
}
else if (currentIsDiscriminatorMappingComplete != isDiscriminatorMappingComplete)
{
throw new InvalidOperationException(
CosmosStrings.IsDiscriminatorMappingCompleteMismatch(
isDiscriminatorMappingComplete, firstEntityType.DisplayName(), entityType.DisplayName(),
currentIsDiscriminatorMappingComplete, container));
}
}
var currentAnalyticalTtl = entityType.GetAnalyticalStoreTimeToLive();
if (currentAnalyticalTtl != null)
{
if (analyticalTtl == null)
{
analyticalTtl = currentAnalyticalTtl;
}
else if (analyticalTtl != currentAnalyticalTtl)
{
var conflictingEntityType = mappedTypes.First(et => et.GetAnalyticalStoreTimeToLive() != null);
throw new InvalidOperationException(
CosmosStrings.AnalyticalTTLMismatch(View on GitHub (pinned to dbf9771522)
Solutions
- Apply the same IsDiscriminatorMappingComplete configuration (complete or not) to every entity type sharing the container.
- Centralize the setting in a shared convention so all roots in a container agree.
- If a type needs a different policy, map it to a separate container.
Example fix
// before (inconsistent setting in one container)
modelBuilder.Entity<Cat>(b => { b.ToContainer("pets"); b.HasDiscriminator(c => c.Kind).IsComplete(); });
modelBuilder.Entity<Dog>(b => { b.ToContainer("pets"); b.HasDiscriminator(d => d.Kind); }); // default (not complete)
// after (both consistent)
modelBuilder.Entity<Cat>(b => { b.ToContainer("pets"); b.HasDiscriminator(c => c.Kind).IsComplete(); });
modelBuilder.Entity<Dog>(b => { b.ToContainer("pets"); b.HasDiscriminator(d => d.Kind).IsComplete(); }); Defensive patterns
Strategy: validation
Validate before calling
// IsDiscriminatorMappingComplete must be consistent within a container.
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))
{
var settings = g.Select(e => e.GetIsDiscriminatorMappingComplete()).Distinct().ToList();
if (settings.Count > 1)
throw new InvalidOperationException($"Inconsistent IsDiscriminatorMappingComplete in {g.Key}.");
} Try / catch
try { context.Database.EnsureCreated(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("IsDiscriminatorMappingComplete"))
{ throw new InvalidOperationException("Apply the same IsDiscriminatorMappingComplete across the shared container.", ex); } Prevention
- Apply IsDiscriminatorMappingComplete via a shared convention per container.
- When adding a type to a shared container, match the existing setting.
- Add a build-time test asserting one IsDiscriminatorMappingComplete value per container.
When it happens
Trigger: Calling IsComplete() (or not) on one entity type in a shared container but configuring the opposite on another; mixing abstract/incomplete discriminator mappings across roots in one container.
Common situations: Adding a new type to a shared container without matching the existing IsDiscriminatorMappingComplete convention; partial refactoring that flips the setting on only some types.
Related errors
- The entity type '{entityType}' is sharing the container '{co
- The entity type '{entityType}' is sharing the container '{co
- The discriminator value for '{entityType1}' is '{discriminat
- 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/09a3a6a30c210de7.
Report an issue: GitHub.