dotnet/efcore · error · InvalidOperationException
The required column ' ' was not present in the results of a…
Error message
The required column '{column}' was not present in the results of a 'FromSql' operation. What it means
Thrown by BufferedDataReader when a non-composed FromSqlRaw/FromSqlInterpolated query returns a result set that is missing at least one column the entity expects. EF looks up each expected column name in the reader's field-name map and reports the first one it cannot find.
Solutions
- Make the raw SQL return every column the entity maps (including key, discriminator, and concurrency token columns).
- Project into an anonymous/non-mapped DTO instead of an entity type to allow fewer columns.
- Use SELECT * (or the specific full column list) so all mapped columns are present.
- Verify column names in SQL match the mapped column names (case sensitivity depends on provider).
Example fix
// before
var users = dbContext.Users
.FromSqlRaw("SELECT Id, Name FROM Users")
.ToList(); // User also maps Email
// after
var users = dbContext.Users
.FromSqlRaw("SELECT Id, Name, Email FROM Users")
.ToList(); Defensive patterns
Strategy: validation
Validate before calling
static void EnsureFromSqlCoversColumns(IModel model, Type entityType, string sql, IReadOnlyList<string> readerColumns)
{
var mapped = model.FindEntityType(entityType)!
.GetProperties().Select(p => p.GetColumnName()).Where(n => n is not null).Cast<string>();
foreach (var c in mapped)
if (!readerColumns.Contains(c, StringComparer.OrdinalIgnoreCase))
throw new InvalidOperationException($"FromSql result is missing mapped column '{c}'.");
} Prevention
- Return every mapped column from raw SQL (or use SELECT *).
- Prefer LINQ over FromSql for entity materialization when possible.
- Project into DTOs when only a subset of columns is needed.
When it happens
Trigger: context.Users.FromSqlRaw("SELECT Id, Name FROM Users") when the User entity also maps a property to column Email. InitializeFields detects FieldCount < _columns.Count and a missing column name.
Common situations: Raw SQL projection that omits a mapped column. Stored procedure that returns a subset of columns. Renaming a column in SQL but not the entity. Composite/TPH queries where a discriminator column is missing.
Related errors
- An error occurred while reading a database value. The…
- An error occurred while reading a database value. The…
- An error occurred while reading a database value. See the…
- The underlying reader doesn't have as many fields as…
- A FromSqlExpression has an invalid arguments expression type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/eb3a4931581faf64.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)