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 property configured. Configure a discriminator property and assign a unique value for this entity type.

What it means

CosmosModelValidator.ValidateSharedContainerCompatibility throws InvalidOperationException(CosmosStrings.NoDiscriminatorProperty) when an instantiable entity type (ClrType.IsInstantiable()) sharing a container with other types has no discriminator property configured. When multiple entity types map to the same container, Cosmos needs a discriminator to tell documents apart, and EF needs to know which property holds it. The check fires only for document roots (not embedded).

Solutions

  1. Configure a discriminator property on every instantiable entity type sharing the container: modelBuilder.Entity<A>().HasDiscriminator(x => x.Type).HasValue("a").
  2. If the types are part of an inheritance hierarchy, configure the discriminator on the root and values on each derived type.
  3. If you did not intend to share a container, give each type its own container via HasContainerName.
  4. Avoid disabling the discriminator convention on types that share a container.

Example fix

// before - throws: A and B share a container with no discriminator
modelBuilder.Entity<A>().ToContainer("Shared");
modelBuilder.Entity<B>().ToContainer("Shared");

// after - configure a discriminator property and value on each
modelBuilder.Entity<A>()
    .ToContainer("Shared")
    .HasDiscriminator(a => a.Kind).HasValue("a");
modelBuilder.Entity<B>()
    .ToContainer("Shared")
    .HasDiscriminator(b => b.Kind).HasValue("b");
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate: every instantiable document-root type in a shared container must have a discriminator property.
static bool AllHaveDiscriminators(IReadOnlyList<IEntityType> types)
    => types.Where(t => t.ClrType.IsInstantiable() && t.GetContainingPropertyName() is null)
        .All(t => t.FindDiscriminatorProperty() is not null);

Prevention

When it happens

Trigger: Two or more concrete entity types mapped to the same container via HasContainerName, none of which calls HasDiscriminator; sharing a container for unrelated types without configuring a discriminator; disabling the default discriminator convention on a shared-container type.

Common situations: Sharing containers between unrelated entity types for cost reasons and skipping discriminator setup; a convention that removes the default discriminator; entity types that used to be in separate containers and were merged.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/c7a7bb2341b4ae4d. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:201

                                partitionKeyStoreNames[i],
                                entityType.GetPartitionKeyPropertyNames()[i],
                                entityType.DisplayName(),
                                storeNames[i]));
                    }
                }
            }

            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));
                }

View on GitHub (pinned to 3a2006ef56)