Blazor is Awesome

Blazor is AwesomeBlazor is AwesomeBlazor is Awesome
  • Sign In
  • Create Account

  • Bookings
  • My Account
  • Signed in as:

  • filler@godaddy.com


  • Bookings
  • My Account
  • Sign out

  • Home
  • Tips and Tricks
  • More
    • Home
    • Tips and Tricks

Blazor is Awesome

Blazor is AwesomeBlazor is AwesomeBlazor is Awesome

Signed in as:

filler@godaddy.com

  • Home
  • Tips and Tricks

Account


  • Bookings
  • My Account
  • Sign out


  • Sign In
  • Bookings
  • My Account

Entity Framework

Entity Framework exposes a subset of SQL Server functionality. It is comprehensive but it has limitations.


Sequence Numbers.

Using integer identity values for your invoice numbers works fine as they are guaranteed to be unique, but they are not guaranteed to be sequential. This can be a problem as your Accountants like to see sequential invoice numbers. Sequence Numbers are guaranteed to be unique and sequential. However, unlike Razor Pages, sequence numbers seem to be ignored in Blazor.  To solve this problem, wrap the sequence call in a SQL Server udf and call it directly from Blazor, as below:

                System.FormattableString s = FormattableStringFactory.Create("GetNextInvSeqValue");

                var rawQuery = ctx.Database.SqlQuery<int>(s);

                var task = rawQuery.ToList();

                iSeqInvNo = task[0];


Tables without Keys.

Entity Framework demands that every table has a key field. On the surface this does not seem to be unreasonable however, if another process creates a table without a key in the database and you need to join to it, you can’t. SQL Server imposes no such limitation. To solve this problem, bypass Entity Framework and execute the join directly, as below:

            List<string> lstEmailRegisteredClientsWithPOSEntry = new List<string>();

            using (var ctx = ContextFactory.CreateDbContext())

            {

                System.FormattableString sFormatedString = FormattableStringFactory.Create("SELECT p.email FROM dbo.tblPerson p INNER JOIN dbo.AspNetUsers a ON p.email = a.email;");

                var rawQuery = ctx.Database.SqlQuery<string>(sFormatedString);

                lstEmailRegisteredClientsWithPOSEntry = rawQuery.ToList();

            }

Initializing and rendering a page

Ordinarily you would put the code to render the page in 'protected override async Task OnInitializedAsync() { }'. But for certain tasks this doesn't work, like JS Interop and NavigateTo, because the page needs to be rendered before these will execute. This becomes problematic when you have to perform complex authentication logic to determine if this user can access this page or if they have to be redirected to the 'AccessDenied' page. You solve this problem by putting this code in the 'protected override async Task OnAfterRenderAsync(bool firstRender)' method.

Align HTML items horizonally

Ue the Bootstrap d-flex class to align groups of HTML items horizontally;


<style>

    .alignHTMLgrouptoright {

        margin-left: auto;

    }


    .alignHTMLgrouptoleft {

        align-self: flex-start; /* Overrides the container's align-items for this specific item */

    }

</style>


            <div class="d-flex">

                <div class="alignHTMLgrouptoleft">

                         group of HTML items to align left

                </div>


                <div class="alignHTMLgrouptoright">

                         group of HTML items to align right

                </div>

            </div>

Progress Bars

Under MVC and Razor Pages implementing Progress Bars is a pain due to the nature of the Client / Server environment, but with Blazor it’s a snap. Using Event Callbacks you can make granular adjustments to the progress bar directly from the service that is performing the work. Set up an Event Handler in the service then register the call back in the page. Then you can call it from the service as many times as you like to move the progress bar. The implementation is simple, elegant and easy.


Example:

Install Blazor Bootstrap.

In the Service:

        public event EventHandler<int> InvProgressChanged;

        private int _currentInvProgress;

Inside your loop calculate the current progress and call:

                    InvProgressChanged?.Invoke(this, _currentInvProgress);


In the Page:

<div class="progress">

    <div class="progress-bar" role="progressbar" style="width: @(_progressInvValue)%" aria-valuenow="@_progressInvValue" aria-valuemin="0" aria-valuemax="100">

        @_progressInvValue%

    </div>

</div>

private int _progressInvValue = 0;

    protected override async Task OnInitializedAsync()

    {

        orders.InvProgressChanged += OnInvProgressChanged!;

        …

     }


    private void OnInvProgressChanged(object sender, int progress)

    {

        _progressInvValue = progress;

        InvokeAsync(StateHasChanged); // Ensure UI update on the correct thread

    }


    public void Dispose()

    {

        orders.InvProgressChanged -= OnInvProgressChanged;

    }



Copyright © 2025 Blazor is Awesome - All Rights Reserved.

Powered by

This website uses cookies.

We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.

Accept