how to call store procedure using EF

mohammad fayaz 0 Reputation points
2024-11-23T13:10:39.1333333+00:00

created parametrized custom data store procedure in using SQL server now I want to call my store procedure using .NET entity framework and get data from it but unable to get data due to various errors

.NET F#
.NET F#
.NET: Microsoft Technologies based on the .NET software framework.F#: A strongly typed, multi-paradigm programming language developed by the F# Software Foundation, Microsoft, and open contributors.
108 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. mohammad fayaz 0 Reputation points
    2024-11-23T13:11:53.1733333+00:00
        public async Task<List<Barelvi.Model.Read.Book>> GetAllBooksAsync(Guid userId)
    
        {
    
            var sql = "EXEC Book_ByUserId @UserId";
    
            var parameter = new SqlParameter("@UserId", userId);
    
            var rawBooks = await BarelviContext.Set<Barelvi.DataAccess.Model.Book>()
    
                .FromSqlRaw(sql, parameter)
    
                .ToListAsync();
    
            // Map the raw BookDto results to your domain model (Barelvi.Model.Read.Book)
    
            var books = rawBooks.Select(b => new Barelvi.Model.Read.Book
    
            {
    
                Id = b.Id,
    
                Name = b.Name,
    
                LanguageId = b.LanguageId,
    
                CategoryId = b.CategoryId,
    
                AuthorId = b.AuthorId,
    
                LanguageName = b.LanguageName,
    
                CategoryName = b.CategoryName,
    
                AuthorName = b.AuthorName,
    
                Status = b.Status,
    
                IsDeleted = b.IsDeleted
    
            })
    
            .ToList();
    
            return books;
    
        }  
    

    this is my code

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.