2

I am using EF Core. I want to extract data from the database using a stored procedure and EF Core. I am getting error for those property whose are not returned from DB. How can I fix this?

Entity:

public class CustomOverview
{
    public int A{ get; set; }
    public int? B{ get; set; }
    public int? C{ get; set; }
    public int? D{ get; set; }
}

Repository code

public async Task<CustomOverview> GetMyData()
{
    return  _dbContext.CustomOverviews.FromSqlInterpolated($"exec spr_myStoredProcedure).AsEnumerable().FirstOrDefault();
}

My stored procedure returns data for Column A, Column B.

When I run the code, I get this exception:

InvalidOperationException: The required column 'C' was not present in the results of a 'FromSql' operation.

How can I resolve it? There is many solution where they suggest to remove property or use [NotMapped] attribute before the property. But my requirement is I need those property for different stored procedure. So I couldn't remove them.

1 Answer 1

3
  1. Create a new class that matches the stored procedure return results.

or

  1. Use traditional data reader to read the stored procedure results into your existing class.

or

  1. Add columns C and D to the stored procedure results, using Null values if appropriate.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.