{"record":{"id":"7d4579b8b16800ea","repo":"dotnet/efcore","slug":"explicitly-named-default-constraints-cannot-be-use","errorCode":null,"errorMessage":"Explicitly named default constraints cannot be used with TPC inheritance or entity splitting. Constraint name: '{explicitDefaultConstraintName}'.","messagePattern":"Explicitly named default constraints cannot be used with TPC inheritance or entity splitting\\. Constraint name: '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Conventions/SharedTableConvention.cs","lineNumber":772,"sourceCode":"        string? schema,\n        Dictionary<(string, string?), (IConventionProperty, StoreObjectIdentifier)> defaultConstraints,\n        in StoreObjectIdentifier storeObject,\n        int maxLength)\n    {\n        var mappedTables = property.GetMappedStoreObjects(StoreObjectType.Table);\n        if (mappedTables.Count() > 1)\n        {\n            // For TPC and some entity splitting scenarios we end up with multiple tables having to define the constraint.\n            // Since constraint name has to be unique, we can't keep the same name for all\n            // Disabling this scenario until we have better way to configure the constraint name\n            // see issue #27970\n            if (property.GetDefaultConstraintNameConfigurationSource() == null)\n            {\n                throw new InvalidOperationException(\n                    RelationalStrings.ImplicitDefaultNamesNotSupportedForTpcWhenNamesClash(constraintName));\n            }\n\n            throw new InvalidOperationException(\n                RelationalStrings.ExplicitDefaultConstraintNamesNotSupportedForTpc(constraintName));\n        }\n\n        if (property.Builder.CanSetAnnotation(RelationalAnnotationNames.DefaultConstraintName, null))\n        {\n            constraintName = Uniquifier.Uniquify(constraintName, defaultConstraints, n => (n, schema), maxLength);\n            property.Builder.HasAnnotation(RelationalAnnotationNames.DefaultConstraintName, constraintName);\n            return constraintName;\n        }\n\n        return null;\n    }\n\n    private void UniquifyTriggerNames(\n        IConventionEntityType entityType,\n        Dictionary<string, (IConventionTrigger, StoreObjectIdentifier)> triggers,\n        in StoreObjectIdentifier storeObject,\n        int maxLength)","sourceCodeStart":754,"sourceCodeEnd":790,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Conventions/SharedTableConvention.cs#L754-L790","documentation":"Thrown by SharedTableConvention.TryUniquifyDefaultConstraint (SharedTableConvention.cs:772-773) when a property is mapped to more than one table (TPC or entity splitting) and the default constraint name IS explicitly set (HasDefaultValueConstraintName was called). EF does not currently support explicitly named default constraints in multi-table scenarios because a single explicit name cannot be shared across multiple tables. This is a known limitation (issue #27970).","triggerScenarios":"Using TPC mapping or entity splitting where a property maps to multiple tables, and calling .HasDefaultValueConstraintName(\"MyConstraint\") — the explicit name applies to all tables but constraint names must be unique per table.","commonSituations":"A developer tries to fix error 495 by explicitly naming the constraint, but hits this error because explicit names are also unsupported in multi-table TPC/splitting. Setting default constraint names globally then switching to TPC.","solutions":["Remove the explicit default constraint name and instead avoid default values on multi-table properties, or configure defaults at the database level via raw SQL migrations.","Switch to TPH or TPT mapping strategy where single-table default constraints work normally.","Remove HasDefaultValueConstraintName from the property and remove the default value, then add the constraint manually in a migration via migrationBuilder.Sql()."],"exampleFix":"// before: explicit default constraint name in TPC (unsupported)\nmodelBuilder.Entity<Base>().UseTpcMappingStrategy();\nmodelBuilder.Entity<Base>().Property(b => b.Status)\n    .HasDefaultValue(0)\n    .HasDefaultValueConstraintName(\"DF_Status\"); // throws\n\n// after: handle defaults manually via migration SQL\n// Remove default from model, add in migration:\n// migrationBuilder.Sql(\"ALTER TABLE DerivedA ADD CONSTRAINT DF_DerivedA_Status DEFAULT 0 FOR Status\");\n// migrationBuilder.Sql(\"ALTER TABLE DerivedB ADD CONSTRAINT DF_DerivedB_Status DEFAULT 0 FOR Status\");","handlingStrategy":"validation","validationCode":"// Detect explicitly named default constraints on multi-table properties.\nforeach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    foreach (var prop in et.GetDeclaredProperties())\n    {\n        if (prop.GetMappedStoreObjects(StoreObjectType.Table).Count() <= 1) continue;\n        if (prop.GetDefaultConstraintNameConfigurationSource() != null)\n            Console.WriteLine($\"Explicit default constraint name on {et.DisplayName()}.{prop.Name} is not supported with TPC/splitting.\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not use HasDefaultValueConstraintName on properties mapped to multiple tables (TPC/splitting).","Handle defaults for TPC columns in migration SQL with per-table unique names.","Consider TPH/TPT when default constraints are essential."],"tags":["default-constraint","tpc","entity-splitting","convention"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}