{"record":{"id":"eb3a4931581faf64","repo":"dotnet/efcore","slug":"the-required-column-column-was-not-present-in","errorCode":null,"errorMessage":"The required column '{column}' was not present in the results of a 'FromSql' operation.","messagePattern":"The required column '(.+?)' was not present in the results of a 'FromSql' operation\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/Internal/BufferedDataReader.cs","lineNumber":1241,"sourceCode":"                    index[_columnNames[i]] = i;\n                }\n\n                return index;\n            }\n        }\n\n        private void InitializeFields()\n        {\n            var fieldCount = FieldCount;\n            if (FieldCount < _columns.Count)\n            {\n                // Non-composed FromSql\n                var readerColumns = _fieldNameLookup.Value;\n\n                var firstMissingColumn = _columns.Select(c => c?.Name).FirstOrDefault(c => c != null && !readerColumns.ContainsKey(c));\n                if (firstMissingColumn != null)\n                {\n                    throw new InvalidOperationException(RelationalStrings.FromSqlMissingColumn(firstMissingColumn));\n                }\n\n                throw new InvalidOperationException(RelationalStrings.TooFewReaderFields(_columns.Count, FieldCount));\n            }\n\n            _columnTypeCases = Enumerable.Repeat(TypeCase.Empty, fieldCount).ToArray();\n            _ordinalToIndexMap = Enumerable.Repeat(-1, fieldCount).ToArray();\n            if (_columns.Count > 0\n                && _columns.Any(e => e?.Name != null))\n            {\n                // Non-Composed FromSql\n                var readerColumns = _fieldNameLookup.Value;\n\n                _indexMap = new int[_columns.Count];\n                var newColumnMap = new ReaderColumn?[fieldCount];\n                for (var i = 0; i < _columns.Count; i++)\n                {\n                    var column = _columns[i];","sourceCodeStart":1223,"sourceCodeEnd":1259,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/Internal/BufferedDataReader.cs#L1223-L1259","documentation":"Thrown in BufferedDataReader.BufferedDataRecord.InitializeFields for a non-composed FromSql query when a column that EF expects (from the entity/shaper) is missing from the reader's column name lookup. The first missing column name is reported. EF cannot map the entity without all required columns, so it aborts with InvalidOperationException.","triggerScenarios":"A FromSqlRaw/FromSqlInterpolated query whose SQL SELECT list omits a column mapped to a required (non-nullable) property; a stored procedure that returns a different projection than the entity; a renamed DB column not matched by the model.","commonSituations":"Writing 'SELECT Id, Name FROM Users' for an entity that also requires Email; calling a stored proc whose result set changed; aliasing a column so its name no longer matches the mapped property; FROM SQL for an entity after adding a new non-nullable property without updating the query.","solutions":["Ensure your raw SQL/stored proc returns every column mapped on the entity (SELECT * is the safest during development).","If only a subset of columns is returned, project into an anonymous type or DTO with .Select(...) instead of materializing the full entity.","Make the missing property nullable or exclude it from the model if it should not be required.","Align column aliases in the SQL with the mapped column names."],"exampleFix":"// before\nvar users = ctx.Users.FromSqlRaw(\"SELECT Id, Name FROM Users\").ToList();\n// entity has a required Email column -> throws\n\n// after\nvar users = ctx.Users.FromSqlRaw(\"SELECT * FROM Users\").ToList();\n// or project a DTO:\nvar dtos = ctx.Users.FromSqlRaw(\"SELECT Id, Name FROM Users\")\n    .Select(u => new UserDto { Id = u.Id, Name = u.Name }).ToList();","handlingStrategy":"validation","validationCode":"var entityType = dbContext.Model.FindEntityType(typeof(User))!;\nvar required = entityType.GetProperties().Where(p => !p.IsNullable).Select(p => p.GetColumnName()).ToList();\n// ensure your raw SQL returns all `required` columns\nstring sql = \"SELECT \" + string.Join(\", \", required) + \" FROM Users\";","typeGuard":null,"tryCatchPattern":"try { return ctx.Users.FromSqlRaw(sql).ToList(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"was not present in the results of a 'FromSql'\"))\n{ _logger.LogError(ex, \"FromSql missing a required column; fix the SELECT list.\"); throw; }","preventionTips":["Use SELECT * (or list all mapped columns) for FromSql that materializes full entities.","Project into DTOs when you intentionally select a subset.","Add a test asserting the raw SQL returns all non-nullable mapped columns."],"tags":["efcore","query","fromsql","schema-mismatch","materialization"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}