locked
Does not contain definition for SQLQuery when call stored procedure into view model RRS feed

  • Question

  • User-1355965324 posted

    I am trying to call a stored procedure into view model But the error  'Does not contain definition for SQLQuery  and no extension method 'SqlQuery error is coming. How can I call a stored procedure into view model with parameter, Please help

          SqlParameter paruserID = new SqlParameter("@UserID", userId);
                SqlParameter paryear = new SqlParameter("@LogYear", lockyear);
                SqlParameter parmonth = new SqlParameter("@LogMonth", lockmonth);
                var result = _db.Database.SqlQuery<AttendanceViewModel>("exec AttProce", paruserID, paryear ,parmonth );

    Friday, October 16, 2020 8:12 AM

All replies

  • User475983607 posted

    This is an Core support forum.  The method is FromSqlRaw() not SqlQuery.

    https://docs.microsoft.com/en-us/ef/core/querying/raw-sql

    https://www.learnentityframeworkcore.com/raw-sql

    Friday, October 16, 2020 12:24 PM
  • User-1355965324 posted

    How can use Fromaqlraw() in my example. I am using only view model. Not model. View model is not registered in my dbcontext.

    Regards

    Pol

    Friday, October 16, 2020 12:36 PM
  • User475983607 posted

    How can use Fromaqlraw() in my example.

    Read the link and follow the reference documentation.  Post your code here if you run into trouble.

    Friday, October 16, 2020 12:43 PM
  • User-474980206 posted

    with ef core, you need to add a DbSet in the context for the sp results. so the code is:

    // no sqlparameters required
    // assume the dataset  AttendanceViewModel added to context
    var result = _db.AttendanceViewModel.FromSqlRaw("exec AttProce {0}, {1}, {2}", userId, lockyear , lockmonth );

    Friday, October 16, 2020 3:27 PM