dotnet/efcore · error · InvalidOperationException
The required column '{column}' was not present in the result
Error message
The required column '{column}' was not present in the results of a 'FromSql' operation. What it means
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.
Source
Thrown at src/EFCore.Relational/Query/Internal/BufferedDataReader.cs:1241
index[_columnNames[i]] = i;
}
return index;
}
}
private void InitializeFields()
{
var fieldCount = FieldCount;
if (FieldCount < _columns.Count)
{
// Non-composed FromSql
var readerColumns = _fieldNameLookup.Value;
var firstMissingColumn = _columns.Select(c => c?.Name).FirstOrDefault(c => c != null && !readerColumns.ContainsKey(c));
if (firstMissingColumn != null)
{
throw new InvalidOperationException(RelationalStrings.FromSqlMissingColumn(firstMissingColumn));
}
throw new InvalidOperationException(RelationalStrings.TooFewReaderFields(_columns.Count, FieldCount));
}
_columnTypeCases = Enumerable.Repeat(TypeCase.Empty, fieldCount).ToArray();
_ordinalToIndexMap = Enumerable.Repeat(-1, fieldCount).ToArray();
if (_columns.Count > 0
&& _columns.Any(e => e?.Name != null))
{
// Non-Composed FromSql
var readerColumns = _fieldNameLookup.Value;
_indexMap = new int[_columns.Count];
var newColumnMap = new ReaderColumn?[fieldCount];
for (var i = 0; i < _columns.Count; i++)
{
var column = _columns[i];View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
var users = ctx.Users.FromSqlRaw("SELECT Id, Name FROM Users").ToList();
// entity has a required Email column -> throws
// after
var users = ctx.Users.FromSqlRaw("SELECT * FROM Users").ToList();
// or project a DTO:
var dtos = ctx.Users.FromSqlRaw("SELECT Id, Name FROM Users")
.Select(u => new UserDto { Id = u.Id, Name = u.Name }).ToList(); Defensive patterns
Strategy: validation
Validate before calling
var entityType = dbContext.Model.FindEntityType(typeof(User))!;
var required = entityType.GetProperties().Where(p => !p.IsNullable).Select(p => p.GetColumnName()).ToList();
// ensure your raw SQL returns all `required` columns
string sql = "SELECT " + string.Join(", ", required) + " FROM Users"; Try / catch
try { return ctx.Users.FromSqlRaw(sql).ToList(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("was not present in the results of a 'FromSql'"))
{ _logger.LogError(ex, "FromSql missing a required column; fix the SELECT list."); throw; } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The underlying reader doesn't have as many fields as expecte
- An error occurred while reading a database value. See the in
- An error occurred while reading a database value. The expect
- An error occurred while reading a database value. The expect
- The required column '{column}' was not present in the result
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/eb3a4931581faf64.
Report an issue: GitHub.