{"record":{"id":"8ce789bdb1c82a8e","repo":"dotnet/efcore","slug":"entitytype1-property1-and-entitytype2-pr-8ce789","errorCode":null,"errorMessage":"'{entityType1}.{property1}' and '{entityType2}.{property2}' are both mapped to column '{columnName}' in '{table}', but are configured with different maximum lengths ('{maxLength1}' and '{maxLength2}').","messagePattern":"'(.+?)\\.(.+?)' and '(.+?)\\.(.+?)' are both mapped to column '(.+?)' in '(.+?)', but are configured with different maximum lengths \\('(.+?)' and '(.+?)'\\)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1550,"sourceCode":"    /// <param name=\"property\">A property.</param>\n    /// <param name=\"duplicateProperty\">Another property.</param>\n    /// <param name=\"columnName\">The column name.</param>\n    /// <param name=\"storeObject\">The identifier of the store object.</param>\n    /// <param name=\"logger\">The logger to use.</param>\n    protected virtual void ValidateCompatible(\n        IProperty property,\n        IProperty duplicateProperty,\n        string columnName,\n        in StoreObjectIdentifier storeObject,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        // NB: Properties can have different nullability, the resulting column will be non-nullable if any of the properties is non-nullable\n\n        var currentMaxLength = property.GetMaxLength(storeObject);\n        var previousMaxLength = duplicateProperty.GetMaxLength(storeObject);\n        if (currentMaxLength != previousMaxLength)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DuplicateColumnNameMaxLengthMismatch(\n                    duplicateProperty.DeclaringType.DisplayName(),\n                    duplicateProperty.Name,\n                    property.DeclaringType.DisplayName(),\n                    property.Name,\n                    columnName,\n                    storeObject.DisplayName(),\n                    previousMaxLength,\n                    currentMaxLength));\n        }\n\n        if (property.IsUnicode(storeObject) != duplicateProperty.IsUnicode(storeObject))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DuplicateColumnNameUnicodenessMismatch(\n                    duplicateProperty.DeclaringType.DisplayName(),\n                    duplicateProperty.Name,\n                    property.DeclaringType.DisplayName(),","sourceCodeStart":1532,"sourceCodeEnd":1568,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1532-L1568","documentation":"EF Core detected two properties (on different entity types sharing the same table) that map to the same column but specify conflicting MaxLength facets. The validator cannot decide which length to use for the physical column, so model finalization aborts. This is thrown from ValidateCompatible during the shared-table (TPH/table-splitting/owned) compatibility check.","triggerScenarios":"A TPH hierarchy (e.g. Student and Teacher both inheriting Person) where each declares an Address property with different HasMaxLength calls; or two entity types mapped to the same table via table splitting / ToTable(t) / owned types whose matching columns set different MaxLength. Fails on first DbContext use or DbContextOptionsBuilder.UseXxx finalization.","commonSituations":"Two developers independently added the same-named property to sibling derived types with different lengths; an owned entity and its owner both configure the shared column; partial model where one side uses data annotations ([MaxLength(100)]) and the other uses Fluent API with a different value; refactoring a property without updating the mirrored one.","solutions":["Pick one canonical MaxLength and apply the same value to both properties via HasMaxLength(n).","If the columns are genuinely different, give them distinct column names with HasColumnName on at least one side, or move one type to its own table via ToTable.","Remove the explicit MaxLength from one property so it inherits the CLR type default, matching the other.","Centralize shared-column facets in a shared base type or an IEntityTypeConfiguration to prevent divergence."],"exampleFix":"// before\nmodelBuilder.Entity<Student>().Property(s => s.Name).HasMaxLength(100);\nmodelBuilder.Entity<Teacher>().Property(t => t.Name).HasMaxLength(200);\n// after (shared 'People' table, one canonical length)\nconst int NameLen = 200;\nmodelBuilder.Entity<Student>().Property(s => s.Name).HasMaxLength(NameLen);\nmodelBuilder.Entity<Teacher>().Property(t => t.Name).HasMaxLength(NameLen);","handlingStrategy":"validation","validationCode":"// At startup (Program.cs / a test) force model finalization to surface shared-column conflicts early.\nusing (var ctx = new MyContext())\n{\n    ctx.Model.ToDebugString(); // triggers ValidateCompatible; throws on MaxLength mismatch\n}\n// Optionally: assert facet parity in a unit test.\nvar a = ctx.Model.FindEntityType(typeof(Student))!.FindProperty(\"Name\")!.GetMaxLength();\nvar b = ctx.Model.FindEntityType(typeof(Teacher))!.FindProperty(\"Name\")!.GetMaxLength();\nDebug.Assert(a == b, $\"MaxLength mismatch: {a} vs {b}\");","typeGuard":null,"tryCatchPattern":"// During app bootstrap, wrap model warm-up to produce a friendly error.\ntry { using var ctx = new MyContext(); _ = ctx.Model; }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"maximum lengths\"))\n{\n    log.Error(\"Shared-column MaxLength mismatch. Align HasMaxLength on sibling entities sharing a table: {Message}\", ex.Message);\n    throw;\n}","preventionTips":["Centralize shared-column facets in a shared base type or IEntityTypeConfiguration<T> so siblings cannot diverge.","Add a unit test that builds the model once at test-startup; any facet mismatch fails fast before runtime.","Use HasColumnName deliberately when two same-named properties must have different facets.","Review migrations diffs after touching any property on an entity in a TPH hierarchy or table-splitting setup."],"tags":["ef-core","model-validation","tpc-tph","shared-table","column-mapping"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}