In this recipe, we will learn how to use stored procedures in EF.
As in the previous recipes, we will continue to use the same CookBook
database and to use xUnit
to create unit test methods.
We will create a repository:
- First, we create two stored procedures in the
CookBook
database:
CREATE PROCEDURE [dbo].[GetBooks] AS BEGIN SET NOCOUNT ON; SELECT [Id],[Name] FROM [dbo].[Book] END GO CREATE PROCEDURE [dbo].[InsertBook] @name varchar(50) AS BEGIN SET NOCOUNT ON; INSERT INTO [dbo].[Book] ([Name]) VALUES (@name) END GO
- After that, we add two methods corresponding to the two stored procedures just created to
DbContext
:
- First, we add
GetBooks
:
- First, we add
- Then, we add
InsertBook
:
- Then, we add
- Next, let's consume these methods: