0

I have an existing stored procedure in my database. I also have an existing database context class as well (fragment code only).

Would you please help advise how do I call my stored procedure from my controller? What is the missing class or property in my model or DatabaseContext?

Thanks very much in advance

Sothun

CREATE PROCEDURE getEmployeeById

@employeeId nvarchar(7)

AS  SELECT * FROM employees WHERE id = @employeeId GO;



namespace Template.Models
{
    public class DatabaseContext : DbContext
    {
        public DatabaseContext() : base("dbConnectionString")
        {

        }
        public DbSet<Parameter> parameter { get; set; }
    //...
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Would point out that generally you probably *wouldn't* execute a stored procedure directly from a controller, as there would be some other layer in between the db and the controllers – Caius Jard Feb 10 '21 at 08:26

1 Answers1

0

In the implement controller you can do like that :

var context = new DatabaseContext(); 
var param = new SqlParameter("@employeeId", employeeId);
var parameters = context.parameter.FromSql("getEmployeeById @employeeId", param).ToList();
  • Hello Pham, thanks for your comment. Where is this Students property is from? and how to define this property? Would you throw some more light here in my given DatabaseContext class above? – Sothun Thay Feb 10 '21 at 09:26
  • @SothunThay I have corrected the code. – Phạm Thế Hùng Feb 10 '21 at 09:32
  • Pham, thanks for answer so far. I understand that I would need to declare another property say public DbSet employee { get; set; } in my DatabaseContext class. How my Employee class would look like then because the I want my store procedure to return true if record is found and false if no record is found – Sothun Thay Feb 10 '21 at 10:18