简体   繁体   中英

Cannot connect to SQL database using Blazor and Entity Framework

I'm trying to setup a simple Blazor website that connects to a SQL Server database and I can't seem to get it to work, I get an error:

System.PlatformNotSupportedException: Strings.PlatformNotSupported_DataSqlClient

when trying to query any of the database entities. Connecting to the same database and using the same DBContext works fine from a unit testing projects, I just can't make it work using Blazor.

In my program.cs I have:

builder.Services.AddDbContext<IntegrationDBContext>(options => options.UseSqlServer(@"MyConnString"));

I even tried using an in-memory provider, which worked fine. I'm using .NET 6 and connecting to an Azure based SQL (although I did try connecting to a LocalDB instance, with the same results).

Thanks!

The MS-SQL Client software uses a TCP socket based protocol that is indeed "not supported" on WebAssembly.

You would need a Db with an HTTP based API but then be very aware that your credentials will be public. A Web based Client is not secure.

This is why all SPA apps that need a backend Database use an API server for Db access. Take a look at the Blazor Wasm + Hosted template, see if that works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM