question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The required column 'Col0' was not present in the results of a 'FromSql' operation.

See original GitHub issue

Executing a stored procedure resulted in the following exception: System.InvalidOperationException: The required column ‘Col0’ was not present in the results of a ‘FromSql’ operation.

The stored procedure in question returns a result with no column name (dbo.sp_describe_first_result_set shows null for name). It seems the reverse engineering is automatically adding in “Col{index}” to the result.

I was expecting an empty partial result class.

PS: it is an easy fix for the developer to provide a column name in the stored procedure but unfortunately this database is read-only (developer lacking schema change permission).

Steps to reproduce

Create a stored procedure like so:

CREATE PROCEDURE dbo.Test()
AS
BEGIN 
	DECLARE @Id INT = 0
	SELECT @Id
END

Reverse engineer the above-stored procedure and notice the generated result class to have a property called Col0. When executing the stored procedure the exception is thrown.

Further technical details

EF Core version in use: 5

EF Core Power Tools version: 2.5.866

Database engine: SQL Server 2019

Visual Studio version: Visual Studio 2019 16.11.8

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ducmancommented, Jan 7, 2022

The example sproc in the 1st post does SELECT @Id. I read that there is no way for EF to get back this data. The only way to get the data is to alter the sproc to give it a column name (i.e. SELECT @Id AS Id).

I think that is why the TestResult is still generated; just that it couldn’t find any columns so the class is empty. Btw, I left out the guts of the method that you can find below.

Before:

public virtual async Task<List<TestResult>> TestAsync(...) 
{ 
  // parameters setup
  var _ = await _context.SqlQueryAsync<TestResult>(...);
  return _;
}

After:

public virtual async Task<int> TestAsync(...)
{
  // parameters setup
  var _ = await _context.Database.ExecuteSqlRawAsync(...);
  return _;
}

Everything is fine for me.

0reactions
ErikEJcommented, Jan 6, 2022

Does Test sproc actually return anything? Can you provide a repro sproc?

Read more comments on GitHub >

github_iconTop Results From Across the Web

The required column was not present in the results of ...
this means that the column 'FirstName' is not being returned in the result set. Do a 'SELECT * FROM TABLE' to solve the...
Read more >
The required column 'FirstName' was not present in ...
Hello,. I have a stored proc that I'd like to return a portion of the fields that are in my model. If I...
Read more >
Asp.Net Core Web Api - System.InvalidOperationException ...
The required column 'ReconciliationResponseId' was not present in the results of a 'FromSql' operation. Posting guidelines
Read more >
The required column 'CLIENT' was not present in ...
The required column 'CLIENT' was not present in the results of a 'FromSql' operation. Hello,. I'm a total newbie at dotnet development.
Read more >
EF error : The required column was not present in the results ...
The required column 'colname' was not present in the results of a 'FromSql' operation. In few scenarios, we want to pull only sub-set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found