Get result from Stored Procedure using Entity Framework DBContext in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

How to get Stored Procedure result and bind into List

I've database as following 

CREATE TABLE [dbo].[Genres](
	[GenreId] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](max) NULL,
	[Description] [nvarchar](max) NULL,
 CONSTRAINT [PK_dbo.Genres] PRIMARY KEY CLUSTERED 
(
	[GenreId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Genres] ON 
GO
INSERT [dbo].[Genres] ([GenreId], [Name], [Description]) VALUES (1, N'Rock', NULL)
GO
INSERT [dbo].[Genres] ([GenreId], [Name], [Description]) VALUES (2, N'Jazz', NULL)
GO
SET IDENTITY_INSERT [dbo].[Genres] OFF
GO
/****** Object:  StoredProcedure [dbo].[getGenre]    Script Date: 24/4/2021 2:59:44 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[getGenre]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

 select [GenreId], [Name] from [dbo].[Genres]
END
GO

Class and method as follow 

    public class getGenre
    {
        public int GenreId { get; set; }
        public string Name { get; set; }

        public static List<getGenre> GetGenre()
        {
            List<getGenre> Genre = new List<getGenre>();
            using (MusicStoreDB entities = new MusicStoreDB())
            {
                string procedureName = "getGenre";

                var getObj = entities.Database.ExecuteSqlCommand(procedureName);
            }

            return Genre;
        }
    }

I need help to complete

public static List<getGenre> GetGenre()
This question does not have replies that have been liked.