dotnet/efcore · error · InvalidOperationException

Both properties ' ' and ' ' on entity type ' ' are mapped…

Error message

Both properties '{property1}' and '{property2}' on entity type '{entityType}' are mapped to '{storeName}'. Map one of the properties to a different JSON property.

What it means

Thrown when two PERSISTED properties on the same entity type map to the same JSON property name. Cosmos documents are JSON objects, so each property under an entity must have a unique JSON name. The validator builds a dictionary keyed by the resolved JSON name and throws on the first collision between two scalar properties.

Solutions

  1. Give each property a distinct JSON name via ToJsonProperty("uniqueName").
  2. Identify the two property names in the error message and search the model config for both, then fix the duplicated target.
  3. If one is a leftover shadow property, remove it instead of renaming.
  4. If a base-type property and a derived property collide, override the JSON name on the derived one.

Example fix

// before
modelBuilder.Entity<User>().Property(u => u.FirstName).ToJsonProperty("Name");
modelBuilder.Entity<User>().Property(u => u.LastName).ToJsonProperty("Name");

// after
modelBuilder.Entity<User>().Property(u => u.FirstName).ToJsonProperty("Name");
modelBuilder.Entity<User>().Property(u => u.LastName).ToJsonProperty("Surname");
Defensive patterns

Strategy: validation

Validate before calling

foreach (var et in dbContext.Model.GetEntityTypes())
{
    var seen = new HashSet<string>();
    foreach (var p in et.GetProperties().Where(x => x.IsPersisted()))
    {
        var json = p.GetJsonPropertyName();
        if (!seen.Add(json)) throw new InvalidOperationException($"JSON name {json} collision on {et.DisplayName()}");
    }
}

Prevention

When it happens

Trigger: Two properties whose ToJsonProperty target resolves to the same string (e.g. both default to "Name" after a rename, or two properties explicitly mapped to "id" / same JSON path). Fires during ValidateDatabaseProperties for every entity.

Common situations: Renaming a CLR property but forgetting to update a manual ToJsonProperty call so it now collides with another; inheriting a base property and re-declaring a same-named one on the derived type with the same JSON mapping; mapping both a shadow property and a real property to the same JSON path.

Related errors


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

Appendix: source

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

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected virtual void ValidateDatabaseProperties(
        IEntityType entityType,
        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
    {
        var properties = new Dictionary<string, IPropertyBase>();
        foreach (var property in entityType.GetProperties().Where(x => x.IsPersisted()))
        {
            var jsonName = property.GetJsonPropertyName();

            if (properties.TryGetValue(jsonName, out var otherProperty))
            {
                throw new InvalidOperationException(
                    CosmosStrings.JsonPropertyCollision(property.Name, otherProperty.Name, entityType.DisplayName(), jsonName));
            }

            properties[jsonName] = property;
        }

        foreach (var navigation in entityType.GetNavigations())
        {
            if (!navigation.IsEmbedded())
            {
                continue;
            }

            var jsonName = navigation.TargetEntityType.GetContainingPropertyName()!;
            if (properties.TryGetValue(jsonName, out var otherProperty))
            {
                throw new InvalidOperationException(
                    CosmosStrings.JsonPropertyCollision(navigation.Name, otherProperty.Name, entityType.DisplayName(), jsonName));

View on GitHub (pinned to 3a2006ef56)