{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1532-L1568","documentation":"Two properties mapped to the same column have different configured maximum lengths. Because they share a physical column, EF cannot pick one max length over the other and refuses to silently truncate, so it throws. Length drives the store type (e.g. nvarchar(N)), so a mismatch would create an ambiguous schema.","triggerScenarios":"ValidateCompatible() compares property.GetMaxLength(storeObject) vs duplicateProperty.GetMaxLength(storeObject) on a duplicate column and they differ (e.g. 50 vs 100). Common with table splitting or TPT where both types own a column of the same name.","commonSituations":"Owned entity and owner both expose a 'Name' string on the shared table with different MaxLength attributes; TPH where derived types restate the length; data annotations vs fluent API disagreement.","solutions":["Set the same MaxLength / string length on both properties (via HasMaxLength, [StringLength] or [MaxLength]).","Explicitly set one to match the other using the fluent API in OnModelCreating.","Remove the explicit length on one side so it inherits the convention default and matches.","If they are truly different columns, remap one to a distinct column name."],"exampleFix":"// before\nmodelBuilder.Entity<User>().Property(u => u.Email).HasMaxLength(100);\nmodelBuilder.Entity<UserProfile>().Property(p => p.Email).HasMaxLength(200); // shared column 'Email'\n\n// after\nmodelBuilder.Entity<User>().Property(u => u.Email).HasMaxLength(200);\nmodelBuilder.Entity<UserProfile>().Property(p => p.Email).HasMaxLength(200);","handlingStrategy":"validation","validationCode":"// Compare max length on every pair of properties sharing a column.\nforeach (var (col, props) in sharedColumnGroups)\n{\n    var lengths = props.Select(p => p.GetMaxLength(storeObject)).Distinct().ToList();\n    if (lengths.Count > 1)\n        Console.WriteLine($\"Column {col} has conflicting max lengths: {string.Join(\", \", lengths)}\");\n}","typeGuard":null,"tryCatchPattern":"try { _ = db.Model; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"different maximum lengths\")) { // align HasMaxLength on the conflicting properties }","preventionTips":["When table-splitting, centralize MaxLength configuration for shared columns.","Use constants for shared lengths (e.g. EmailMaxLength) and reference them from both entities.","Add a unit test that asserts shared columns have matching facets."],"tags":["model-validation","column-mapping","max-length","relational"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}