How to execute stored procedure using blazor

blazor entity-framework-core stored-procedures

Question

I have been looking for some sample which show that the execution of stored procedure using blazor.

So far i haven't seen anything related to execution of stored procedure using blazor.


Here is the parameter with SP

 MSKCIS.UpdateDischargeBoardData    
        @ClientVisitGuid  ,
        @CurrentLocationGuid  ,
        @DischargeDateExp ,
        @DischargeTimeExp  ,
        @DischargeDtmExp  ,
        @userId     
1
1
10/23/2019 9:39:43 PM

Accepted Answer

In EF Core 3.0 , you need to use the FromSqlRaw extension method to execute a stored procedure.

Here is a simple working demo on getting the user data from the database by stored procedures .

User model

 public partial class Users
{
    [Key]
    public int PkId { get; set; }
    public string UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Supervisor { get; set; }
    public DateTime CreationDate { get; set; }
    public string CreationUser { get; set; }
}

UserManagementContext.cs

public partial class UserManagementContext : DbContext
{
    public UserManagementContext(DbContextOptions<UserManagementContext> options): base(options)
    {}

    public virtual DbSet<Users> Users { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=UserManagementDb;Trusted_Connection=True;ConnectRetryCount=0");
        }
    }
}

UserService.cs

public class UserService
{
    private readonly UserManagementContext _context;

    public UserService(UserManagementContext context)
    {
        _context = context;
    }

    public async Task<Users[]> GetUsersAsync()
    {
        Users[] u;

        u = _context.Users
            .FromSqlRaw("EXECUTE GetUserByUserId {0}", "User1")
            .ToArray();

        return u;
    }
}

ConfigureServices method in Startup.cs

public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<UserManagementContext>();

        services.AddRazorPages();
        services.AddServerSideBlazor();
        services.AddSingleton<WeatherForecastService>();

        services.AddTransient<UserService>();  
    }

Index.razor

@page "/"
@using Models
@using Data
@inject UserService us

<form method="post">
<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>UserID</th>
            <th>Name</th>
            <th>Supervisor</th>
            <th>Ersteller</th>
            <th>Erstelldatum</th>
            <th>Optionen</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var user in users)
        {
            <tr>
                <td>@user.PkId</td>
                <td>@user.UserId</td>
                <td>@user.FirstName @user.LastName</td>
                <td>@user.Supervisor</td>
                <td>@user.CreationUser</td>
                <td>@user.CreationDate</td>
                <td>
                    <a>Delete Account</a><br />
                    <a asp-page="/Edit"> Edit Account</a>
                </td>
            </tr>
        }
    </tbody>
</table>
</form>

 @code {

  Users[] users;

  protected override async Task OnInitializedAsync()
  {
    users = await us.GetUsersAsync();
  }
}
1
10/15/2019 6:41:08 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow