[英]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:我正在尝试设置一个简单的 Blazor 网站,该网站连接到 SQL 服务器数据库,但我似乎无法让它工作,我收到一个错误:
System.PlatformNotSupportedException: Strings.PlatformNotSupported_DataSqlClient
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.连接到同一个数据库并使用相同的
DBContext
在单元测试项目中工作正常,我只是无法使用 Blazor 使其工作。
In my program.cs I have:在我的 program.cs 中,我有:
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).
我正在使用 .NET 6 并连接到基于 Azure 的 SQL (尽管我确实尝试连接到 LocalDB 实例,结果相同)。
Thanks!谢谢!
The MS-SQL Client software uses a TCP socket based protocol that is indeed "not supported" on WebAssembly. MS-SQL 客户端软件使用基于 TCP 套接字的协议,该协议在 WebAssembly 上确实“不支持”。
You would need a Db with an HTTP based API but then be very aware that your credentials will be public.您将需要一个带有基于 HTTP 的 API 的数据库,但请注意您的凭据将是公开的。 A Web based Client is not secure.
基于 Web 的客户端不安全。
This is why all SPA apps that need a backend Database use an API server for Db access.这就是为什么所有需要后端数据库的 SPA 应用程序都使用 API 服务器进行 Db 访问。 Take a look at the Blazor Wasm + Hosted template, see if that works for you.
查看 Blazor Wasm + Hosted 模板,看看它是否适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.