1

I am working on an asp.net core application where I am using EF for database operations. In some cases, I need to use Stored procedures. So when I tried to do so, I get errors. The following is the code I used,

var result = await _context.Query<EmployeeResult>().FromSqlRaw("[usp_getEmployeedetails] @employeeID,@purpose", new SqlParameter("@employeeID", employeeID), new SqlParameter("@purpose", purpose)).ToListAsync(); 

Here the "EmployeeResult" is a ViewModel. Inside the Sp, I am using multiple tables to create the result select query.

The error message as follows "Cannot create a DbSet for 'EmployeeResult' because this type is not included in the model for the context."

Can anyone help me to resolve the issue?

1 Answers1

1

Add this code to you dbcontext

public DbSet<EmployeeResult> EmployeeResults { get; set; }
Afshin
  • 1,405
  • 10
  • 18
  • Does this EmployeeResult need to be present in the DB? – Eldho George Sep 25 '20 at 14:24
  • No. Just class or viewmodel you are using like this should be added into your context. – Afshin Sep 25 '20 at 14:26
  • Does the scaffolding the DB context again will remove these changes? Because in some cases the tables may alter or new tables may come. Since we are on the initial stage of the development. – Eldho George Sep 25 '20 at 14:33
  • No need to scaffolding after adding this Dbset. Just build again your project. New scaffolding does not remove this. – Afshin Sep 25 '20 at 14:45
  • Thanks, Afshin. Also if the stored procedure returns multiple select queries then how to handle that situation? – Eldho George Sep 25 '20 at 14:56
  • Your welcome. for multiple result set in ef core look at this : https://stackoverflow.com/questions/41740636/working-with-multiple-resultset-in-net-core – Afshin Sep 25 '20 at 15:06