How can I call a SQL Stored Procedure using EntityFramework 7 and Asp.Net 5

后端 未结 5 517
夕颜
夕颜 2020-11-29 10:48

For last couple of days I am searching for some tutorials about how to call a Stored Procedure from inside a Web API controller method using

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 11:12

    DbContext has a Database property, which holds a connection to the database that you can do whatever you want with:

    context.Database.SqlQuery("exec [dbo].[GetFoo] @Bar = {0}", bar);
    

    However, rather than doing this in your Web Api actions, I would suggest either adding a method to your context or to whatever service/repository that interacts with your context. Then just call this method in your action. Ideally, you want to keep all your SQL-stuff in one place.

提交回复
热议问题