dotnet/efcore · error · InvalidOperationException

Both entity type '{entityType1}' and '{entityType2}' were co

Error message

Both entity type '{entityType1}' and '{entityType2}' were configured to use '{sproc}', stored procedure sharing is not supported. Specify different names for the corresponding stored procedures.

What it means

Thrown by ValidateStoredProcedureCompatibility when two entity types with different root types are mapped to the same stored procedure. Stored procedure sharing is only permitted within a single inheritance hierarchy (same root type, e.g. TPH); unrelated types cannot reuse one sproc.

Source

Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:553

        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
        => ValidateStoredProcedureCompatibility(mappedTypes, storedProcedure, logger);

    /// <summary>
    ///     Validates that a stored procedure is not shared across unrelated entity types.
    /// </summary>
    /// <param name="mappedTypes">The entity types mapped to the stored procedure.</param>
    /// <param name="storedProcedure">The stored procedure identifier.</param>
    /// <param name="logger">The logger to use.</param>
    protected virtual void ValidateStoredProcedureCompatibility(
        IReadOnlyList<IEntityType> mappedTypes,
        in StoreObjectIdentifier storedProcedure,
        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
    {
        foreach (var mappedType in mappedTypes)
        {
            if (mappedTypes[0].GetRootType() != mappedType.GetRootType())
            {
                throw new InvalidOperationException(
                    RelationalStrings.StoredProcedureTableSharing(
                        mappedTypes[0].DisplayName(),
                        mappedType.DisplayName(),
                        storedProcedure.DisplayName()));
            }
        }
    }

    private static void ValidateSproc(
        IStoredProcedure sproc,
        string mappingStrategy,
        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
    {
        var entityType = sproc.EntityType;
        var storeObjectIdentifier = sproc.GetStoreIdentifier();

        var primaryKey = entityType.FindPrimaryKey();
        if (primaryKey == null)

View on GitHub (pinned to dbf9771522)

Solutions

  1. Give each unrelated entity type a distinct stored procedure name.
  2. Only share a sproc name across entities that belong to the same inheritance hierarchy.
  3. Audit all ToStoredProcedure/Insert/Update/Delete calls for duplicate names across non-hierarchy types.

Example fix

// before
modelBuilder.Entity<Blog>().InsertStoredProcedure("sp_Save");
modelBuilder.Entity<Post>().InsertStoredProcedure("sp_Save"); // unrelated

// after
modelBuilder.Entity<Blog>().InsertStoredProcedure("sp_InsertBlog");
modelBuilder.Entity<Post>().InsertStoredProcedure("sp_InsertPost");
Defensive patterns

Strategy: validation

Validate before calling

var byName = new Dictionary<string, string>();
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
    foreach (var s in new[] { et.GetInsertStoredProcedure(), et.GetUpdateStoredProcedure(), et.GetDeleteStoredProcedure() }.Where(s => s != null))
    {
        var id = s.GetStoreIdentifier();
        if (byName.TryGetValue(id.Name, out var other) && other != et.GetRootType().Name)
            throw new InvalidOperationException($"Sproc {id.Name} shared across unrelated types");
        byName[id.Name] = et.GetRootType().Name;
    }
}

Try / catch

try { _ = ctx.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains("stored procedure sharing is not supported")) { /* rename the colliding sproc */ }

Prevention

When it happens

Trigger: Two independent entities both calling .InsertStoredProcedure("sp_Save") with the identical name; accidental name collision across unrelated entities.

Common situations: Naming conventions that produce the same sproc name for different tables; copy-pasting sproc config between entities.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/afb5991ff96a4b80. Report an issue: GitHub.