Can Blazor Webassembly use EntityFramework without the hosting option?

Bernhard Sumser 101 Reputation points
2023-01-15T21:09:13.1966667+00:00

Hello everyone!

Regarding my previous question How can I use Blazor for my project? I used the last days to play around with Azure and Blazor (what a rhyme).

I like both so far but I have a question which I could not find an answer: Can Blazor Webassembly use the Azure SQL Database if it is not hosted? ("hosted" means the option if you setup a new Blazor Webassembly project in Visual Studio).

What I did

  1. I created a new Blazor Webassembly app by following this tutorial Secure an ASP.NET Core Blazor WebAssembly standalone app with Azure Active Directory B2C and succeeded.
  2. I created a Azure SQL Database by using the SQL Server Object Explorer from Visual Studio (that basically opens a browser to create it in Azure). After the creation I was not able to see it in the SQL Server Object Explorer . After a search I proceeded with the next step.
  3. I used that tutorial Add a connection to Azure SQL Database to connect the SQL database. (I had a issue to apply the firewall rules because the database was not public available in the network section). But after successfully adding it I was not able to proceed the Add migration part. There was no dbContext to find.
    After some search I found that article: Cannot connect to SQL database using Blazor and Entity Framework

The article is a bit old, but is this still valid? Should Blazor Webassembly not use the Entity Framework?
If this is not longer valid, how can I implement the Azure SQL Database as Connected Service inside my solution?
I followed several articles and created a simple model

ProductDto.cs

#nullable disable

namespace BlazorTest.Shared
{
    internal class ProductDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public float? Price { get; set; }
    }
}

and a context ProductContext.cs

using Microsoft.EntityFrameworkCore;

namespace BlazorTest.Shared.Data
{
    internal class ProductContext : DbContext
    {
        internal DbSet
Azure Database Migration service
{count} votes

Accepted answer
  1. Rijwan Ansari 706 Reputation points Microsoft MVP
    2023-01-17T01:39:13.2666667+00:00

    Yes you can.

    Blazor WebAssembly can be deployed as static files without the need for server-side rendering.

    We can publish the Blazor applications as like static files without the requirement of a web server or its interactions, since WebAssembly is a technology that executes entirely in the client browser. Although we can do advanced logic in client side using C# in the browser, however, to interact with data, database or other services, this type uses standard web technologies HTTP services. 

    And to communicate with backend or Database we use Web API. Therefore, we will have separate solution/project for Web API where you can use entity framework or any ORM.

    For more details:

    [https://rijsat.com/2022/11/07/what-is-blazor-web-development/


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 28,196 Reputation points
    2023-01-16T19:50:30.55+00:00

    Blazor WASM, which runs in the WASM sandbox, does not have network access, so sql database drivers will not work. The HttpClient library in blazor calls JavaScript to do the actual network requests..

    a WASM app should use a webapi to access a database. if you need local storage there are blazor wrappers for the browser storage.