{"record":{"id":"25ab171c99509098","repo":"dotnet/efcore","slug":"the-operation-executedelete-is-being-applied-on","errorCode":null,"errorMessage":"The operation 'ExecuteDelete' is being applied on the table '{tableName}' which contains data for multiple entity types. Applying this delete operation will also delete data for other entity type(s), hence it is not supported.","messagePattern":"The operation 'ExecuteDelete' is being applied on the table '(.+?)' which contains data for multiple entity types\\. Applying this delete operation will also delete data for other entity type\\(s\\), hence it is not supported\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteDelete.cs","lineNumber":100,"sourceCode":"                tableExpression = tableExpression is JoinExpressionBase join\n                    ? join.Update(unwrappedTableExpression)\n                    : unwrappedTableExpression;\n                var newTables = selectExpression.Tables.ToList();\n                newTables[tableIndex] = tableExpression;\n\n                // Note that we need to keep the select mutable, because if IsValidSelectExpressionForExecuteDelete below returns false,\n                // we need to compose on top of it.\n                selectExpression.SetTables(newTables);\n            }\n\n            // Finally, check if the provider has a native translation for the delete represented by the select expression.\n            // The default relational implementation handles simple, universally-supported cases (i.e. no operators except for predicate).\n            // Providers may override IsValidSelectExpressionForExecuteDelete to add support for more cases via provider-specific DELETE syntax.\n            if (IsValidSelectExpressionForExecuteDelete(selectExpression))\n            {\n                if (AreOtherNonOwnedEntityTypesInTheTable(entityType.GetRootType(), targetTable))\n                {\n                    throw new InvalidOperationException(\n                        RelationalStrings.ExecuteDeleteOnTableSplitting(unwrappedTableExpression.Table.SchemaQualifiedName));\n                }\n\n                selectExpression.ReplaceProjection([]);\n                selectExpression.ApplyProjection();\n\n                return new DeleteExpression(unwrappedTableExpression, selectExpression);\n            }\n        }\n\n        // We can't translate to a simple delete (e.g. the provider doesn't support one of the clauses).\n        // As a fallback, we place the original query in a Contains subquery, which will get translated via the regular entity equality/\n        // containment mechanism (InExpression for non-composite keys, Any for composite keys)\n        var pk = entityType.FindPrimaryKey();\n        if (pk == null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.ExecuteOperationOnKeylessEntityTypeWithUnsupportedOperator(","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteDelete.cs#L82-L118","documentation":"After IsValidSelectExpressionForExecuteDelete succeeds, TranslateExecuteDelete verifies the target table does not store rows for any other non-owned entity type (AreOtherNonOwnedEntityTypesInTheTable). If the table is shared (table splitting between multiple principal entity types), a single DELETE would corrupt the other type's data, so EF refuses.","triggerScenarios":"Two or more entity types mapped to the same table (shared table, distinct keys/columns), and ExecuteDelete targeting one of them would delete rows that also belong to the others. Common with table splitting configurations (entity A and entity B both ToTable(\"shared\")).","commonSituations":"Table-splitting (multiple entities sharing a row to work around column limits or to model optional sections); owning entity and a sibling entity mapped to the same table; refactoring a table into multiple entities without changing the physical table.","solutions":["Map each entity type to its own table so deletes are not ambiguous.","Load entities and use RemoveRange + SaveChanges, which understands shared-table mappings and updates only the relevant columns (or use raw SQL constrained by the entity's key).","Restructure the shared table: make one entity the owner and the others owned types so EF coordinates writes."],"exampleFix":"// before (two entities share 'orders' table -> 596)\nmodelBuilder.Entity<Order>().ToTable(\"orders\");\nmodelBuilder.Entity<OrderAudit>().ToTable(\"orders\");\nawait db.Orders.Where(o => o.Closed).ExecuteDeleteAsync();\n// after (separate tables, or use SaveChanges)\nmodelBuilder.Entity<OrderAudit>().ToTable(\"order_audits\");\nawait db.Orders.Where(o => o.Closed).ExecuteDeleteAsync();","handlingStrategy":"validation","validationCode":"// Detect shared tables (multiple non-owned entity types on the same table) before ExecuteDelete.\nvar et = db.Model.FindEntityType(typeof(TEntity))!;\nvar root = et.GetRootType();\nforeach (var mapping in root.GetTableMappings())\n{\n    var others = mapping.Table.EntityTypeMappings\n        .Where(m => !m.EntityType.IsInOwnershipPath() && m.EntityType != root)\n        .Select(m => m.EntityType.Name);\n    if (others.Any())\n        throw new InvalidOperationException(\n            $\"Table {mapping.Table.Name} is shared with {string.Join(\", \", others)}; ExecuteDelete is ambiguous.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Map each entity type to its own table when bulk delete is needed.","Use SaveChanges for shared-table entities so EF coordinates the writes.","Document shared-table mappings clearly so developers know ExecuteDelete is unsafe."],"tags":["efcore","executedelete","table-splitting","mapping"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}