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; }
    //...
    }
}
1
  • 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 Commented Feb 10, 2021 at 8:26

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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?
@SothunThay I have corrected the code.
Pham, thanks for answer so far. I understand that I would need to declare another property say public DbSet<Employee> 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.